Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android靜態傳參數

Android靜態傳參數

編輯:關於Android編程

一、新建OtherActivity.java

 

public class OtherActivity extends Activity {

	public static String name;
	public static int age;
	private TextView textview;
	
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);
        
        textview = (TextView)this.findViewById(R.id.msg);
        textview.setText("name:"+name+",age:"+age);
    }

	
}

定義靜態變量name,age.

 

 

二、在MainActivity.java,跳轉頁面的時候調用class OtherActivity的靜態變量name、age參數傳入

 

public class MainActivity extends Activity {

	Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)this.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(MainActivity.this,OtherActivity.class);
			OtherActivity.age = 23;
			OtherActivity.name ="deng";
			startActivity(intent);
			}
		});
    }
}

 

 

三、附上xml

1)activity_main.xml

 

 

 

2)other.xml

 



    

    



3)AndroidManifest.xml

            


 






 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved