一.建立一個Button監聽器,使Button能開啟另一個activity並傳值
阿新 • • 發佈:2019-01-27
建立一個button監聽器,並使監聽器能開啟另一個activity並傳值
一.建立一個Button的OnClick監聽器
//這個屬於一個內部類 class MyButtonListener implements OnClickListener{ // public void onClick(View v) { // TODO Auto-generated method stub //建立一個Intent的例項 Intent intent = new Intent(); //設定從哪個activity啟動哪個activity intent.setClass(HelloWorldActivity.this, otherHelloWorld.class); //把一個值寫入到Intent中 intent.putExtra("Text", "測試值"); //啟動另一個activity HelloWorldActivity.this.startActivity(intent); }
二.把監聽事件繫結到按鈕
//獲取按鈕的ID
mybutton = (Button)findViewById(R.id.myButton);
//繫結監聽事件
mybutton.setOnClickListener(new MyButtonListener());
三.在另一個activity中讀取intent值
//建立一個Intent例項儲存傳送過來的值 Intent myIntent = getIntent(); //讀取Intent的值 String TextValue = myIntent.getStringExtra("Text");