利用XML标记描绘应用界面
XML使用方式:
<TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
或者
<TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"> </TextView>
XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这是个线性布局, match_parent意思是与上级视图保持一致--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这是个文本视图,名字叫做tv_hello,显示的文字内容为“Hello World!” --> <TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </LinearLayout> </LinearLayout>
使用Java代码书写程序逻辑
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 当前的页面布局采用的是res/layout/activity_main.xml setContentView(R.layout.activity_main); // 获取名叫tv_hello的TextView控件,注意添加导包语句import android.widget.TextView; TextView tv_hello = findViewById(R.id.tv_hello); // 设置TextView控件的文字内容 tv_hello.setText("你好,世界"); } }
页面跳转功能
// 活动页面跳转,从MainActivity跳到Main2Activity startActivity(new Intent(MainActivity.this, Main2Activity.class)); // 简化后的跳转代码 startActivity(new Intent(this, Main2Activity.class));
延时跳转
@Override protected void onResume() { super.onResume(); goNextPage(); // 跳到下个页面 } // 跳到下个页面 private void goNextPage() { TextView tv_hello = findViewById(R.id.tv_hello); tv_hello.setText("3秒后进入下个页面"); // 延迟3秒(3000毫秒)后启动任务mGoNext new Handler(Looper.myLooper()).postDelayed(mGoNext, 3000); } private Runnable mGoNext = new Runnable() { @Override public void run() { // 活动页面跳转,从MainActivity跳到Main2Activity startActivity(new Intent(MainActivity.this, Main2Activity.class)); } };
浙公网安备 33010602011771号