Android studio關於點擊事件后的頁面跳轉,選擇完成后返回(onActivityResult)


我這個人喜歡直接上代碼,在代碼中說明更方便,更直接。

首先在.xml中設置一個button按鈕,和一個EditText框,並分別做好id號。

這里我以籍貫測試對象。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_text1a1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/City"
android:text="籍貫"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextCity"
/>

</LinearLayout>

在.java中,將上面的id進行注冊,初始化。
之后設置button的點擊事件,intent為跳轉方法

public class text1A1 extends AppCompatActivity {
private Button City;
private EditText editTextCity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text1a1);
init();
City.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(text1A1.this,text1A2.class);
startActivityForResult(intent,0);

}
});
}


使用onActivityResult方法,對值的調用並返回,這里我設置“giguan”為值的接口返回
 protected void onActivityResult(int A1,int A2,Intent data){
super.onActivityResult(A1,A2,data);
if (A1 == 0 && A2 ==0);
Bundle extras=data.getExtras();
String giguan =extras.getString("giguan");
editTextCity.setText(giguan);


}

private void init() {
City=(Button)findViewById(R.id.City);
editTextCity=(EditText)findViewById(R.id.editTextCity);
}
}

新建textA2,在textA2的.xml表中建立ListView控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_text1_a2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView">

</ListView>

</LinearLayout>


在textA2.java表中,依然注冊控件,初始化。對應前面的接口,創建數據
public class text1A2 extends AppCompatActivity {
private ListView listView;
private String[] giguan={"非洲","歐洲","亞洲"};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text1_a2);
init();
ArrayAdapter adapter =new ArrayAdapter(text1A2.this,
android.R.layout.simple_list_item_1,giguan);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =getIntent();
Bundle bundle =new Bundle();
bundle.putString("giguan",giguan[position]);
intent.putExtras(bundle);
text1A2.this.setResult(0,intent);
text1A2.this.finish();

}
});
}

private void init() {
listView=(ListView)findViewById(R.id.listView);
}
}


 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM