Android 创建一个activity 及创建Intent对象跳转界面并传值

一、创建一个activity

步骤1、创建一个java类,该类必须继承Android.app.Activity

步骤2、重写该类的onCreate(Bundle)方法

步骤3、创建一个布局文件res/layout

步骤4、在onCreate方法中调用setContentView(布局文件),绑定布局

步骤5、在Androidmainifest.xml文件中注册新的窗体类

步骤6、进行窗体开发。


二、创建Intent跳转对象并传值

1、①new Intent(原界面,目标界面)

Intent intent=new Intent(MainActivity.this,ResultActivity.class);

  ② Intent intent = new Intent();

intent.setClass(原界面,目标界面);

2、Intent对象传值

intent.putExtra("user",user);

3、跳转

startActivity(intent);



4、另一个Activity获取传入的值

Intent intent=getIntent();

int user=intent.getIntExtra("user", -1);
//int型的 若传过来的值为空则给赋值-1

三、自定义对话框

                    android:name=".ResultActivity"
            android:label="结果" 
            android:theme="@android:style/Theme.Dialog">
           
               
                 
           

       


MainActivity.java

package com.example.demo3_guss;

import java.util.Random;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

	private RadioButton rdoJD, rdoST, rdoB;
	private Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.rdoJD = (RadioButton) findViewById(R.id.rdoJD);
		this.rdoST = (RadioButton) findViewById(R.id.rdoST);
		this.rdoB = (RadioButton) findViewById(R.id.rdoB);

		this.btn = (Button) findViewById(R.id.btn);

		this.btn.setOnClickListener(new Viewocl());
	}

	private class Viewocl implements View.OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btn:

				int user = 0;
				if (rdoJD.isChecked()) {
					// Toast.makeText(MainActivity.this,
					// "你选中了剪刀!",Toast.LENGTH_SHORT).show();
					user = 1;
				} else if (rdoST.isChecked()) {
					// Toast.makeText(MainActivity.this,
					// "你选中了石头!",Toast.LENGTH_SHORT).show();
					user = 2;
				} else {
					// Toast.makeText(MainActivity.this, "你选中了布!",
					// Toast.LENGTH_SHORT).show();
					user = 3;
				}

				
				
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, ResultActivity.class);
				intent.putExtra("user", user);
				startActivity(intent);
			
			
				break;

			default:
				break;
			}

		}
	}

	
}
ResultActivity.java

package com.example.demo3_guss;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ResultActivity extends Activity {
	
	private TextView txtShow;
	private Button btnClose;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);
		
		this.txtShow=(TextView) findViewById(R.id.txtShow);
		this.btnClose=(Button) findViewById(R.id.btnClose);
		
		this.btnClose.setOnClickListener(new Viewocl());
		
		Intent intent = getIntent();

		int user = intent.getIntExtra("user", -1);

		int cpu = new Random().nextInt(3) + 1;

		String message="";
		if (user == 3 && cpu == 1) {
			message="用户:" + convert(user) + "  VS    " + convert(cpu)
					+ "电脑\n对不起,输了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"用户:" + convert(user) + "  VS    " + convert(cpu)
//							+ "电脑\n对不起,输了!", Toast.LENGTH_SHORT).show();
		} else if (user > cpu) {
			
			message="用户:" + convert(user) + "  VS    " + convert(cpu)
					+ "电脑\n恭喜你,赢了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"用户:" + convert(user) + "  VS    " + convert(cpu)
//							+ "电脑\n恭喜你,赢了!", Toast.LENGTH_SHORT).show();
		} else if (user < cpu) {
			message="用户:" + convert(user) + "  VS    " + convert(cpu)
					+ "电脑\n对不起,输了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"用户:" + convert(user) + "  VS    " + convert(cpu)
//							+ "电脑\n对不起,输了!", Toast.LENGTH_SHORT).show();
		} else if (user == 1 && cpu == 3) {
			message="用户:" + convert(user) + "  VS    " + convert(cpu)
					+ "电脑\n恭喜你,赢了!";
//			Toast.makeText(
//					ResultActivity.this,
//					"用户:" + convert(user) + "  VS    " + convert(cpu)
//							+ "电脑\n恭喜你,赢了!", Toast.LENGTH_SHORT).show();
		} else {
			message="用户:" + convert(user) + "  VS    " + convert(cpu)
					+ "电脑\n平局!";
//			Toast.makeText(
//					ResultActivity.this,
//					"用户:" + convert(user) + "  VS    " + convert(cpu)
//							+ "电脑\n平局!", Toast.LENGTH_SHORT).show();
		}
		
		this.txtShow.setText(message);

	}

	public String convert(int num) {

		String result = "";

		switch (num) {
		case 1:
			result = "剪刀";
			break;

		case 2:
			result = "石头";
			break;
		case 3:
			result = "布";
			break;
		default:
			break;
		}

		return result;
	}

	public class Viewocl implements View.OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btnClose:
				finish();
				break;

			default:
				break;
			}
		}
		
		
	}
}

activity_main.xml



    

    

        

        

        
    
    
    


activity_result.xml



    


AndroidManifest.xml




    

    
        
            
                

                
            
        
        
            
                
                  
            
        
    





你可能感兴趣的:(Android)