您的位置:首页 > 移动开发 > Android开发

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

2016-05-04 16:56 816 查看
一、创建一个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

三、自定义对话框

<activity

android:name=".ResultActivity"

android:label="结果"

android:theme="@android:style/Theme.Dialog">

<intent-filter>

<action android:name="android.intent.action.VIEW"/>

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请您出拳" />

<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RadioButton
android:id="@+id/rdoJD"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="剪刀"
android:textSize="18dp" />

<RadioButton
android:id="@+id/rdoST"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="石头"
android:textSize="18dp" />

<RadioButton
android:id="@+id/rdoB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="布"
android:textSize="18dp" />
</RadioGroup>

<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="出拳"
android:textSize="18dp"
></Button>

</LinearLayout>


activity_result.xml

<?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" >

<TextView
android:id="@+id/txtShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
/>
<Button
android:id="@+id/btnClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="确定"
/>

</LinearLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo3_guss"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultActivity"
android:label="结果"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: