Android监听小实例
Android监听小实例
Android监听小实例,参考 Android基础教程第三版,修订版中内容。
相关下载:
Android基础教程(第3版 修订版).pdf 下载地址:
//main.xml主页面布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:layout_gravity="center"
- android:paddingLeft="10px"
- android:paddingRight="10px"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20px"
- android:layout_marginBottom="20dip"
- android:text="@string/text_lable"
- android:layout_gravity="center"
- />
- <TableLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- >
- <TableRow>
- <Button
- android:id="@+id/continue_button"
- android:text="@string/contiune_label"
- />
- <Button
- android:id="@+id/newgame_button"
- android:text="@string/newgame_label"
- />
- </TableRow>
- <TableRow>
- <Button
- android:id="@+id/about_button"
- android:text="@string/about_label"
- />
- <Button
- android:id="@+id/exit_button"
- android:text="@string/exit_label"
- />
- </TableRow>
- </TableLayout>
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">书上案例</string>
- <string name="about_text">\
- 如果你想显示成对话框的形式的话就在AndroidManifest.xml里面有个
- android:name=\".About\"那个Activity中加上一句\n
- android:theme=\"@android:style/Theme.Dialog\"\n
- </string>
- <string name="about_title">About_Title</string>
- <string name="text_lable">Welcome to Game world</string>
- <string name="about_label">About</string>
- <string name="contiune_label">Continue</string>
- <string name="newgame_label">New Game</string>
- <string name="exit_label">Exit</string>
- </resources>
- package sn.len.bookdemoabout;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- public class BookDemoAboutActivity extends Activity implements OnClickListener
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过findviewById()方法找个各个按钮对应的ID
- View continueButton=findViewById(R.id.continue_button);
- View newGameButton=findViewById(R.id.newgame_button);
- View aboutButton=findViewById(R.id.about_button);
- View exitButton=findViewById(R.id.exit_button);
- //为各个按钮分别注册监听
- continueButton.setOnClickListener(this);
- newGameButton.setOnClickListener(this);
- aboutButton.setOnClickListener(this);
- exitButton.setOnClickListener(this);
- }
- @Override
- public void onClick(View v)
- {
- //给指定的Button添加自己的处理内容
- switch(v.getId())
- {
- case R.id.continue_button:break;
- case R.id.newgame_button:break;
- case R.id.about_button:Intent i=new Intent(this,About.class);startActivity(i);break;
- //解析new Intent(this,About.class) this作当前,About.cass做目标,通过当前找到目标(通过本类Activity可以打开About这个Activity)
- //this是指本类(BookDemoAboutActivity)对象 -->当前
- //About.class是指About这个类的路径com.sn.len.About -->目标
- case R.id.exit_button:Intent exit=new Intent(this,Exit.class);startActivity(exit);break;
- }
- }
- }
- package sn.len.bookdemoabout;
- import android.app.Activity;
- import android.os.Bundle;
- public class About extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.about);//设置 显示指定的布局内容about.xml
- }
- }
//about.xml文件
- <?xml version="1.0" encoding="utf-8" ?>
- <ScrollView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dip"
- >
- <TextView
- android:id="@+id/about_content"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/about_text"
- />
- </ScrollView>
- package sn.len.bookdemoabout;
- import android.app.Activity;
- import android.os.Bundle;
- public class Exit extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.about);
- }
- }
|
评论暂时关闭