`

Android登录界面开发及响应;页面跳转;传参

    博客分类:
  • JAVA
 
阅读更多
import android.app.Activity;

import android.app.AlertDialog;


import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;

import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;



public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取事件源对象
        Button btLogin=(Button)this.findViewById(R.id.btLogin);
        Button btReset=(Button)this.findViewById(R.id.btReset);
        //事件处理方法
        OnClickListener ocl=new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Login(arg0);
				
			}
        	
        };
        btLogin.setOnClickListener(ocl);
        btReset.setOnClickListener(ocl);
  }
    public void Login(View arg0){
    	EditText name=(EditText)findViewById(R.id.username);
    	EditText pwd=(EditText)findViewById(R.id.pwd);
    	Button bt=(Button)findViewById(arg0.getId());
    	String text=bt.getText().toString();
    	if(text.equals("登录")){
    		System.out.println("------>>>>"+name);
    		Log.d("MY QQ","------->>>"+name);
    		//获取输入的账号和密码
    		String name1=name.getText().toString();
    		String pwd1=pwd.getText().toString();
    		if(name1.equals(pwd1)){
    			// 判断
    				// 显示获取到的用户信息
    				Toast toast = Toast.makeText(this, "账号:" + name1
    						+ "\r\n密码:" + pwd1, 20);
    				toast.show();// 显示信息
    				new AlertDialog.Builder(this).setTitle("登录").setMessage("账号:"+name1+"\r\n密码:"+pwd1).setNegativeButton("确定",null).show();
    								//提示框
 				     Intent intent=new Intent();
 				     intent.setClass(this, SecondActivity.class);
 				     intent.putExtra("Username", name1);
 				     intent.putExtra("Userpwd",pwd1);
    		}
    		else{
    			Toast toast=Toast.makeText(this,"您输入的账号和密码不一致,请重新输入",10);
    			toast.show();
    		}
    	}
    	else{
    		name.setText("");
    		pwd.setText("");
    	}
    	
    	
   	
    
    }
    	


    private Toast setNegativeButton(String string, Object object) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

 一直以来十分艳羡看着大神将自己编辑的游戏以及开发的软件下载到自己的手机上为所欲为,于是这几天在学习了几天后的Android后,决定小试牛刀一把。登陆界面的开发其实并不困难,困难的是如何熟悉组件,以及灵活运用各组件的关系,毕竟登陆界面的实现有两种方法一个是编辑代码,这个其实有点繁琐,另一个就是类似见过的MFC功能般的拖曳组件就可以了。

以下是登陆界面的事件机制相应的实现:

import android.app.Activity;

import android.app.AlertDialog;


import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;

import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;



public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取事件源对象
        Button btLogin=(Button)this.findViewById(R.id.btLogin);
        Button btReset=(Button)this.findViewById(R.id.btReset);
        //事件处理方法
        OnClickListener ocl=new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Login(arg0);
				
			}
        	
        };
        btLogin.setOnClickListener(ocl);
        btReset.setOnClickListener(ocl);
  }
    public void Login(View arg0){
    	EditText name=(EditText)findViewById(R.id.username);
    	EditText pwd=(EditText)findViewById(R.id.pwd);
    	Button bt=(Button)findViewById(arg0.getId());
    	String text=bt.getText().toString();
    	if(text.equals("登录")){
    		System.out.println("------>>>>"+name);
    		Log.d("MY QQ","------->>>"+name);
    		//获取输入的账号和密码
    		String name1=name.getText().toString();
    		String pwd1=pwd.getText().toString();
    		if(name1.equals(pwd1)){
    			// 判断
    				// 显示获取到的用户信息
    				Toast toast = Toast.makeText(this, "账号:" + name1
    						+ "\r\n密码:" + pwd1, 20);
    				toast.show();// 显示信息
    				new AlertDialog.Builder(this).setTitle("登录").setMessage("账号:"+name1+"\r\n密码:"+pwd1).setNegativeButton("确定",null).show();
    								//提示框
 				     Intent intent=new Intent();
 				     intent.setClass(this, SecondActivity.class);
 				     intent.putExtra("Username", name1);
 				     intent.putExtra("Userpwd",pwd1);
    		}
    		else{
    			Toast toast=Toast.makeText(this,"您输入的账号和密码不一致,请重新输入",10);
    			toast.show();
    		}
    	}
    	else{
    		name.setText("");
    		pwd.setText("");
    	}
    	
    	
    	
    
    }
    	


    private Toast setNegativeButton(String string, Object object) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

 标红色字迹的是跳转页面的实现,当然实现跳转页面,需要新建一个Android页面才可以(注意添加到res下的Androidmanifest.xml,否则无法实现)

import android.app.Activity;


import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;


public class SecondActivity extends Activity{
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		Intent intent=this.getIntent();
		String name=intent.getStringExtra("Username");
		String pwd=intent.getStringExtra("Userpwd");
		new AlertDialog.Builder(this).setTitle("QQ").setMessage("账号:"+name+"\r\n密码:"+pwd);
		//获取到显示文本的对象。
				//TextView textTest = (TextView)this.findViewById(R.id.);
				//将TextView原来的值加上账号,一起显示出来
				//textTest.setText(textTest.getText().toString()+name);
	}
	
	
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

 每个XML文件对应一个.java文件,xml文件创建也是与上面一样的。以上涉及了两个页面传递参数,想要对象会怎样呢,需要用到两种方法,一种是Android自带,一个java自带,通常来说还是使用Android自带的比较保险不会出现问题。



 

 

 

 

 

  • 大小: 495 KB
  • 大小: 217.2 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics