Activity学习日记(五)

                                   Activity学习日记(五)

  • 实现密码记住功能
package com.hq.myapp;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;


public class LoginActivity extends Activity {
	private final String TAG = "MyApp";
	Button  btn_login;
	Button  btn_exit;
	EditText et_username;
	EditText et_passwd;
	CheckBox cb_auto_login;
	CheckBox cb_mark_pwd;
	
	SharedPreferences  myappSetting;
	SharedPreferences.Editor myEditor;
	
	void myToast()
	{
		//来个吐司--用于调试/提示
		//自定义一个吐司
		Toast t = Toast.makeText(LoginActivity.this, "自定义吐司", Toast.LENGTH_SHORT);
		t.setGravity(Gravity.CENTER, 0, 0);
		LinearLayout layout = 	(LinearLayout) t.getView();  //得到一个view
		ImageView image = new ImageView(LoginActivity.this);
		image.setImageResource(R.drawable.head3);
		layout.addView(image);
		t.show();
	}
	
	//必须有一个监听器
	OnClickListener btnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.d(TAG, "点击了,666");
			
			// 1,获取两个输入框的内容
			String username = et_username.getText().toString().trim();
			String passwd = et_passwd.getText().toString().trim();
			// 2, 比对内容
			if(!username.isEmpty() && !passwd.isEmpty()){
				if(username.equals("root") && passwd.equals("123456")){
					myToast();
					// 3, 比对密码成功就进入另外一个界面xxx.class是所有类中一个程序,记录对应类的信息
					
					/*
					// 第一种启动方式--显示启动
					Intent  intent = new Intent(LoginActivity.this, RegiterActivity.class);
					startActivity(intent);
					*/
					
					//如果记住密码被选中
					if(cb_mark_pwd.isChecked())
					{
						//将密码和用户名保存起来
						myEditor.putString("usernameKey", username);
						myEditor.putString("passwdKey", passwd);
						myEditor.putBoolean("isMarkKey", cb_mark_pwd.isChecked());
						myEditor.putBoolean("isAutoKey", cb_auto_login.isChecked());
						
						myEditor.commit();
					}else{
						myEditor.putString("usernameKey", "");
						myEditor.putString("passwdKey", "");
						myEditor.putBoolean("isMarkKey", cb_mark_pwd.isChecked());
						myEditor.putBoolean("isAutoKey", cb_auto_login.isChecked());
						
						myEditor.commit();
					}
					
					//第二种启动方式---隐式启动
					Intent intent = new Intent(); //已经加了默认的分类
					intent.setAction("com.hq.test.action");
					intent.addCategory(Intent.CATEGORY_DEFAULT);
					intent.addCategory("com.hq.my.cateroy");
					intent.setData(Uri.parse("MyData://my.host/10010"));
					startActivity(intent);
					
				}else{
					Toast.makeText(LoginActivity.this, "用户或者密码错误", 1000).show();
				}
				
				
			}else{
				Toast.makeText(LoginActivity.this, "用户或者密码不能为空", 1000).show();
			}
			
			
			
			
		}
	}; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//其实该代码已经对xml中所有的标签进行分析,已经new所有控件
		setContentView(R.layout.activity_login);
		//初始化sharepreferce
		myappSetting = getSharedPreferences("myapp_setting", MODE_PRIVATE);
		myEditor = myappSetting.edit();
		
		
		initUI();
		
		//需要将xml文件中设置读出,并设置对应ui
		//先判断是否有数据
		if(!myappSetting.getAll().isEmpty()){
			
			String name = myappSetting.getString("usernameKey", "george");
			et_username.setText(name);
			
			String passwd = myappSetting.getString("passwdKey", "1");
			et_passwd.setText(passwd);
			
			cb_mark_pwd.setChecked(myappSetting.getBoolean("isMarkKey", false));
			cb_auto_login.setChecked(myappSetting.getBoolean("isAutoKey", false));
		}
		
	}
	private void initUI() {
		// TODO Auto-generated method stub
		cb_auto_login = (CheckBox) findViewById(R.id.cb_auto_login);
		cb_mark_pwd = (CheckBox) findViewById(R.id.cb_mark_pwd);
		
		et_passwd = (EditText) findViewById(R.id.et_passwd);
		et_username = (EditText) findViewById(R.id.et_username);
		
		//获取到ui控件
		btn_login = (Button) findViewById(R.id.btn_login);
		//绑定事件源和监听器
		btn_login.setOnClickListener(btnClickListener);
		
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				// 先要创造建造工--创建dialog对象的对象
				AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
				//设置icon图标
				builder.setIcon(R.drawable.button_share);
				//设置标题栏
				builder.setTitle("退出对话框");
				// 设置消息体
				builder.setMessage("你真的那么忍心就退出吗");
				// 设置两个按钮
				builder.setNegativeButton("再玩会", null);
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						System.exit(1);
					}
				});
				//创造对话框
				builder.create().show();
				
				
				
			}
		});
	}

	
}
package com.hq.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class RegiterActivity extends Activity{
	private final String TAG = "MyApp";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		Log.d(TAG, "----------onCreate()------------");
		
	}
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d(TAG, "----------onStart()------------");
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "----------onResume()------------");
	}
	
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "----------onRestart()------------");
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d(TAG, "----------onPause()------------");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(TAG, "----------onStop()------------");
	}
	
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.d(TAG, "----------onDestroy()------------");
	}
}
package com.hq.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ThirdActivity extends Activity implements OnClickListener{
	Button btn_test;
	Button btn_test2;
	EditText et_show_text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acitivy_third);
		
		initUI();
		
		Intent intent = getIntent();
		
		String dataString = intent.getData().toString();
		String scheme = intent.getData().getScheme();
		String host = intent.getData().getHost();
		String path = intent.getData().getPath();
		
		
		et_show_text.setText("dataString = " + dataString + "\n" + 
				"scheme = " + scheme + "\n" + 
				"host = " + host + "\n" + 
				"path = " + path 		
				);	
		
		
	}

	private void initUI() {
		// TODO Auto-generated method stub
		
		btn_test = (Button) findViewById(R.id.btn_test);
		btn_test.setOnClickListener(this);
		
		btn_test2 = (Button) findViewById(R.id.btn_test2);
		btn_test2.setOnClickListener(this);
		
		et_show_text = (EditText) findViewById(R.id.et_show_text);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// 区分不同的按钮
		switch (v.getId()) {
		case R.id.btn_test:
			Toast.makeText(ThirdActivity.this, "测试1按钮点击666", 1000).show();
			break;
		case R.id.btn_test2:
			Toast.makeText(ThirdActivity.this, "测试2按钮点击666", 1000).show();		
			break;
		default:
			break;
		}
		
	}
	
	
}
<?xml version="1.0" encoding="utf-8"?>
<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/editText1"
        android:hint="输入文字"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="34dp"
        android:text="用户名"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_user_name"
        android:layout_alignBottom="@+id/tv_user_name"
      	android:layout_toRightOf="@id/tv_user_name"
        android:layout_marginLeft="30dp"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tv_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_user_name"
        android:layout_below="@+id/tv_user_name"
        android:layout_marginTop="59dp"
        android:text="密  码"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/et_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_passwd"
        android:layout_alignLeft="@+id/et_username"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/cb_mark_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_passwd"
        android:layout_centerVertical="true"
        android:text="记住密码" />

    <CheckBox
        android:id="@+id/cb_auto_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/cb_mark_pwd"
        android:layout_marginLeft="45dp"
        android:layout_toRightOf="@+id/cb_mark_pwd"
        android:text="自动登录" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/cb_mark_pwd"
        android:layout_below="@+id/cb_mark_pwd"
        android:layout_marginTop="67dp"
        android:text="登录" />

    <Button
        android:id="@+id/btn_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_login"
        android:layout_alignRight="@+id/et_username"
        android:layout_marginRight="16dp"
        android:text="退出" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp" />

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/weixin_42471952/article/details/81114757