实例1:创建简单的Android应用

在Android编程中,第一步是创建一个简单的应用。以下是一个基本的Android应用示例,它包含一个文本视图,显示“Hello, World!”。

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

res/layout/activity_main.xml文件中,添加以下代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/hello_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:layout_gravity="center" />

</FrameLayout>

这个例子展示了如何创建一个简单的Android应用,并添加一个文本视图来显示消息。

实例2:使用布局文件

在Android应用中,布局文件用于定义用户界面。以下是一个使用线性布局的例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

这个布局文件定义了一个线性布局,其中包含一个文本输入框和一个提交按钮。

实例3:处理用户输入

为了处理用户输入,我们需要在Activity中添加事件监听器。以下是一个简单的例子:

Button button = findViewById(R.id.button_submit);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText editText = findViewById(R.id.input_text);
        String inputText = editText.getText().toString();
        Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
    }
});

在这个例子中,当用户点击提交按钮时,程序会显示一个Toast消息,其中包含用户输入的文本。

实例4:使用Intent传递数据

在Android应用中,Intent用于在不同组件之间传递数据。以下是一个使用Intent传递数据的例子:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", "value");
startActivity(intent);

在这个例子中,我们创建了一个Intent,用于启动SecondActivity,并传递了一个键值对。

实例5:使用SharedPreferences存储数据

SharedPreferences用于在应用之间存储数据。以下是一个使用SharedPreferences存储数据的例子:

SharedPreferences sharedPreferences = getSharedPreferences("MyApp", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", "value");
editor.apply();

在这个例子中,我们使用SharedPreferences存储了一个键值对。

实例6:使用数据库存储数据

对于更复杂的数据存储需求,我们可以使用SQLite数据库。以下是一个使用SQLite数据库存储数据的例子:

public class MyDatabaseHelper extends SQLiteOpenHelper {
    // ... 数据库初始化代码 ...
}

在这个例子中,我们创建了一个自定义的SQLiteOpenHelper类,用于初始化数据库。

实例7:使用网络请求获取数据

在Android应用中,我们可以使用网络请求来获取数据。以下是一个使用HTTP请求获取数据的例子:

String url = "http://example.com/data";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
// ... 处理响应数据 ...

在这个例子中,我们使用HTTP请求获取了一个URL的数据。

实例8:使用RecyclerView显示列表数据

RecyclerView是一个用于显示列表数据的组件。以下是一个使用RecyclerView显示列表数据的例子:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(data));

在这个例子中,我们使用RecyclerView显示了一个列表数据。

实例9:使用Fragment管理界面

Fragment是用于管理界面组件的组件。以下是一个使用Fragment管理界面的例子:

Fragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();

在这个例子中,我们使用Fragment来管理一个界面组件。

实例10:使用Material Design创建美观的界面

Material Design是Google推出的一套设计规范。以下是一个使用Material Design创建美观界面的例子:

<androidx.cardview.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:padding="16dp" />

</androidx.cardview.widget.CardView>

在这个例子中,我们使用CardView创建了一个美观的界面组件。

通过以上10个实用实例,你可以从零开始学习Android编程,并轻松掌握开发技巧。希望这些例子能帮助你更好地理解Android编程。