Activity之間進行傳值

首先在Activity之間進行跳轉要在清單文件中進行註冊例題是從MainActivity傳值跳轉到ResultActivity

具體例題在360雲盤中http://yunpan.cn/cZNcQw3cf3Spb  訪問密碼 9d90

  <activity android:name= "com.example.activityTestRP.ResultActivity" >
           
        </ activity>
標註:< intent-filter>
                < action android:name ="android.intent.action.MAIN" />

                < category android:name ="android.intent.category.LAUNCHER" />
            </ intent-filter>
這兩行是表示是否要創建快捷圖標
  android:label = "@string/app_name"表示這個界面的標籤
android:icon = "@drawable/ic_launcher"表示這個界面的圖片
如果沒有定義這兩個的話,直接使用的是默認的標籤和圖片
xml文件
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
    xmlns:tools= "http://schemas.android.com/tools"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"
    android:orientation= "vertical"
     >
    <TextView
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_gravity ="center_horizontal"
        android:textSize ="28sp"
        android:textColor ="#ff0000"
        android:text ="人品測試" />
    <EditText
        android:id ="@+id/et_name"
        android:layout_width ="match_parent"
        android:layout_height ="wrap_content"
        android:hint ="請輸入姓名:" />
    <Button    
        android:id ="@+id/test"
        android:layout_width ="match_parent"
        android:layout_height ="wrap_content"
        android:text ="開始測試" />
</ LinearLayout>
Java文件
public class MainActivity extends Activity implements OnClickListener {
        private EditText et_name ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super .onCreate(savedInstanceState);
              setContentView(R.layout. activity_main );
       Button test = (Button) findViewById(R.id. test );
        et_name= (EditText) findViewById(R.id. et_name );
       test.setOnClickListener( this );
       }
        @Override
        public void onClick(View v) {
              String name= et_name.getText().toString().trim();
               if (TextUtils.isEmpty(name)){
                      //跳出一個警告
                      new AlertDialog.Builder( this).setMessage( "名字不能爲空" ).show();
              }
               else {
                     Intent intent = new Intent(this , ResultActivity. class);
                     //將值傳到intent中
                     intent.putExtra( "name" ,name);
                     startActivity(intent);
              }
       }
}
第二個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:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:textSize= "28sp"
    android:textColor= "#ff0000"
    android:text= "你的人品值"
    android:layout_gravity= "center_horizontal" />
   <TextView
    android:id= "@+id/tv_result"
    android:layout_width= "match_parent"
    android:layout_height= "wrap_content"
    />
     <!--進度條-->
   <ProgressBar
       android:id ="@+id/pb_testRP"
       style= "?android:attr/progressBarStyleHorizontal"
       android:layout_width ="fill_parent"
       android:layout_height ="wrap_content"
       android:max ="100" />
</ LinearLayout>

Java文件
public class ResultActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
               // TODO Auto-generated method stub
               super .onCreate(savedInstanceState);
              setContentView(R.layout. result );
              TextView result =(TextView) findViewById(R.id. tv_result );
              
              Intent intent = getIntent();
              //獲得前面傳過來的name
              String name= intent. getStringExtra( "name");
              
              Random random = new Random();
               int rp = random.nextInt(101);
              
              result.setText(name+ "您的人品值爲:" +rp);
              
              ProgressBar pb = (ProgressBar) findViewById(R.id. pb_testRP );
              //設置進度值
              pb.setProgress(rp);
       }
       
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章