android编程举例4.doc

上传人:夺命阿水 文档编号:21156 上传时间:2022-07-11 格式:DOC 页数:17 大小:92KB
返回 下载 相关 举报
android编程举例4.doc_第1页
第1页 / 共17页
android编程举例4.doc_第2页
第2页 / 共17页
android编程举例4.doc_第3页
第3页 / 共17页
android编程举例4.doc_第4页
第4页 / 共17页
android编程举例4.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《android编程举例4.doc》由会员分享,可在线阅读,更多相关《android编程举例4.doc(17页珍藏版)》请在课桌文档上搜索。

1、word表格布局:M 功能描述:主界面上有一个“登录按钮,点击“登录按钮后打开一个新的Activity;新的Activity上面有输入用户名和密码的控件,在用户关闭这个Activity后,将用户输入的用户名和密码传递到主界面中。 编程要点:主界面的Activity命名为MainActivity;启动新的Activity命名为UserLoginActivity;分别使用显示启动和隐式启动的方式,启动新的Activity;用户名中不能出现“符号,同时长度不超过12个字符;密码使用密码文本显示方式,即显示为“*,同时只能为数字; 返回的用户名和密码要以Toast的方式显示出来; MainActivi

2、ty和UserLoginActivity中各个生命周期的回调函数中要以Log.i方式显示日志信息。两个布局xml代码类似上面的布局,故省略,自己模仿MainActivity.javapublic class MainActivity extends Activity protected int SUBACTIVITY1 = 1;/唯一标示子activity标识码private static String TAG = MainActivity;/用来在运行时显示activity日志,即上面要求的第五条Button button1,button2;public void onCreate(Bund

3、le savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.i(TAG, (1) onCreate();/日志上显示onCreatebutton1 = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);button1.setOnClickListener(new View.OnClickListener() public void onCl

4、ick(View v) Intent intent = new Intent(MainActivity.this,UserLoginActivity.class);Toast.makeText(getApplicationContext(), 显示启动, Toast.LENGTH_SHORT).show();startActivityForResult(intent, SUBACTIVITY1); );button2.setOnClickListener(new View.OnClickListener()public void onClick(View v) Intent intent =

5、new Intent(.test.nchu);Toast.makeText(getApplicationContext(), 隐式启动, Toast.LENGTH_SHORT).show();startActivityForResult(intent, SUBACTIVITY1););protected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data);if (resultCode = RESULT_O

6、K) Uri uriData = data.getData();Toast.makeText(getApplicationContext(), uriData.toString(),Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), 用户名和密码为空, Toast.LENGTH_SHORT).show();/可视生命周期开始时被调用,对用户界面进展必要的更改 public void onStart() super.onStart(); Log.i(TAG, (2) onStart(); /在onSta

7、rt()后被调用,用于恢复onSaveInstanceState()保存的用户界面信息 public void onRestoreInstanceState(Bundle savedInstanceState) super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, (3) onRestoreInstanceState(); /在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源 public void onResume() super.onResume(); Log.i(TAG, (4) onResum

8、e(); / 在onResume()后被调用,保存界面信息 public void onSaveInstanceState(Bundle savedInstanceState) super.onSaveInstanceState(savedInstanceState); Log.i(TAG, (5) onSaveInstanceState(); /在重新进入可视生命周期前被调用,载入界面所需要的更改信息 public void onRestart() super.onRestart(); Log.i(TAG, (6) onRestart(); /在活动生命周期完毕时被调用,用来保存持久的数据或

9、释放占用的资源。 public void onPause() super.onPause(); Log.i(TAG, (7) onPause(); /在可视生命周期完毕时被调用,一般用来保存持久的数据或释放占用的资源 public void onStop() super.onStop(); Log.i(TAG, (8) onStop(); /在完全生命周期完毕时被调用,释放资源,包括线程、数据连接等 public void onDestroy() super.onDestroy(); Log.i(TAG, (9) onDestroy(); UserLoginActivity.javapubli

10、c class UserLoginActivity extends Activity EditText username,password;Button submit,reset;private static String TAG = MainActivity;protected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login); Log.i(TAG, (1) onC

11、reate();username=(EditText)this.findViewById(R.id.username);password=(EditText)this.findViewById(R.id.password);submit=(Button)this.findViewById(R.id.submit);reset=(Button)this.findViewById(R.id.reset);username.setFocusable(true);username.setOnKeyListener(new OnKeyListener()public boolean onKey(View

12、 v, int keyCode, KeyEvent event) / TODO Auto-generated method stubint unicodeChar = event.getUnicodeChar();if(unicodeChar= 64)return true;else return false;);submit.setOnClickListener(new View.OnClickListener() public void onClick(View v) / TODO Auto-generated method stubString name =username.getTex

13、t().toString();String pass=password.getText().toString();String uriString=用户名:+name+密码:+pass; Uri data = Uri.parse(uriString); Intent result = new Intent(null, data); setResult(RESULT_OK, result);finish(););reset.setOnClickListener(new View.OnClickListener() public void onClick(View v) / TODO Auto-g

14、enerated method stubsetResult(RESULT_CANCELED, null);finish(););下面和MainActivity.java中的一样都是设置activity生命周期在log中显示/显式启动要加的activity/隐式启动,名字和MainActivity.java中的隐式启动里的名字一样编程建立一个简单的进程内服务,实现比拟两个整数大小的功能。服务提供Intpare(Int, Int)函数,输入两个整数,输出较大的整数。 主界面的Activity命名为MainActivity;提供两个EditText,分别输入两个整数;提供一个Botton,启动比拟过

15、程;提供一个TextView,显示较大的整数;分别使用启动方式和绑定方式使用Service;分别使用Handle和AsyncTask更新TextView中的内容。启动方式MainActivity.javapublic class MainActivity extends Activity public static int maxNum;public static Handler handler=new Handler();private static TextView result=null;private static Button pare=null;private static But

16、ton reset=null;private static EditText one=null;private static EditText two=null;public static void UpdateGUI(int refreshDouble)maxNum=refreshDouble;handler.post(RefreshLable); private static Runnable RefreshLable=new Runnable()public void run() result.setText(String.valueOf(maxNum); public void onC

17、reate(Bundle savedInstanceState) final Bundle mybundle=new Bundle(); super.onCreate(savedInstanceState); setContentView(R.layout.main); final Intent intent=new Intent(MainActivity.this,pareService.class); result=(EditText)findViewById(R.id.result); pare=(Button)findViewById(R.id pare); reset=(Button

18、)findViewById(R.id.reset); one=(EditText)findViewById(R.id.one); two=(EditText)findViewById(R.id.two); pare.setOnClickListener(new OnClickListener() public void onClick(View v) mybundle.putString(one, one.getText().toString(); mybundle.putString(two, two.getText().toString(); intent.putExtras(mybund

19、le); startService(intent); ); reset.setOnClickListener(new OnClickListener() public void onClick(View v) one.setText(null); two.setText(null); result.setText(null); ); pareService.javapublic class pareService extends Service private Thread workThread;Bundle bundle=null;int one=0,two=0;public void on

20、Create() super.onCreate(); workThread = new Thread(null,backgroudWork,WorkThread);public void onStart(Intent intent, int startId) super.onStart(intent, startId); bundle=intent.getExtras(); String c1=bundle.getString(one); String c2=bundle.getString(two); if(!c1.toString().equals()&!c2.toString().equ

21、als() one=Integer.parseInt(c1); two=Integer.parseInt(c2); if (!workThread.isAlive() workThread.start(); public IBinder onBind(Intent intent) return null;private Runnable backgroudWork = new Runnable()public void run() int randomDouble =Intpare(one,two);MainActivity.UpdateGUI(randomDouble);stopSelf()

22、;int Intpare(int a,int b)if(a=b)return a;elsereturn b;这样并没有完,还要在中参加service名字,如application。和前面的代码一样绑定启动MainActivity.Javapublic class MainActivity extends Activity private boolean isBound = false;private pareService pareService;int a=0,b=0; public void onCreate(Bundle savedInstanceState) super.onCreat

23、e(savedInstanceState); setContentView(R.layout.main); final TextView result=(TextView)findViewById(R.id.result); Button pare=(Button)findViewById(R.id pare); Button reset=(Button)findViewById(R.id.reset); final EditText one=(EditText)findViewById(R.id.one); final EditText two=(EditText)findViewById(

24、R.id.two); if(!isBound)Intent serviceIntent=new Intent(MainActivity.this,pareService.class);bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);isBound=true; pare.setOnClickListener(new OnClickListener()public void onClick(View v) String c1=one.getText().toString();String c2=two.getText(

25、).toString();if(!c1.equals()&!c2.equals()a=Integer.parseInt(c1);b=Integer.parseInt(c2);result.setText(String.valueOf(pareService.Intpare(a, b); ); reset.setOnClickListener(new OnClickListener() public void onClick(View v) one.setText(null); two.setText(null); result.setText(null); ); private Service

26、Connection mConnection=new ServiceConnection()public void onServiceConnected(ponentName name, IBinder service) pareService=(pareService.LocalBinder)service).getService();public void onServiceDisconnected(ponentName name) / TODO Auto-generated method stubpareService=null;pareService.javapublic class

27、pareService extends Service private final IBinder mBinder=new LocalBinder();class LocalBinder extends Binder pareService getService() return pareService.this; public IBinder onBind(Intent intent) / TODO Auto-generated method stubreturn mBinder;public int Intpare(int a,int b)if(a=b)return a;elsereturn b;最后中参加service名字,和显式启动一样。17 / 17

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号