我灣來隱式地傳遞兩個字符串或變量從dialoguebox到由另一活動,並在同一時間打開該活動中使用。傳遞一個字符串或一變量從dialoguebox到活動的Android
dialoguebox
String city = addresses.get(0).getLocality();
String category = "Hazard";
AlertDialog.Builder prompt = new
AlertDialog.Builder(MapsActivity.this);
prompt.setCancelable(false)
.setPositiveButton("Mineralization", new
DialogInterface.OnClickListener() {
.setPositivetiveButton("Geohazard", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
startActivity(new
Intent(getApplicationContext(),MapsActivity.class));
//i want to pass the String city and category here
}
}
)
AlertDialog alert = prompt.create();
alert.setTitle("Please select an option");
alert.show();
TextView myTextView = (TextView) findViewById(R.id.PSString);
myTextView.setText(city);
新活動
package com.example.boneyflesh.homepage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class GeohazardResults extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geohazard_results);
}
}
我如何做到這一點?我該寫在onClickListener什麼,我該怎麼辦就新的活動,這樣我可以得到的字符串?
很好,謝謝你。其他答案也可以工作,但我選擇了這個答案,因爲對於像我這樣的初學者來說,它是明確而簡單易懂的。 :豎起大拇指: – Boneyflesh
@Boneyflesh你應該考慮提供有用的答案(除了選擇「獲勝者」) – theblitz
@theblitz我確實誇大了他們,但它表示一旦我的聲望超過15 T_T,公共狀態將會改變。 – Boneyflesh