我实际上采用此代码形式vogella.com。
我想在第二个活动中单击按钮时将数据从第二个活动传递到第一个活动。为了确保传递的值是正确的,我希望它在返回第一个活动时显示为吐司。
这是我在firstactivity_layout上的代码。 RES / activity_intentintent.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:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="60dip"
android:text="First Activity. Press button to call second activity"
android:textSize="20sp" >
</TextView>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Calling an intent" >
</Button>
</LinearLayout>
这是布局的第二个代码。 RES / picklayout.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" >
<EditText
android:id="@+id/input11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/input22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
这是第一个布局的活动代码,IntentintentActivity.java
public class IntentintentActivity extends Activity {
Button btn1;
private static final int REQUEST_CODE = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentintent);
}
public void onClick(View view) {
Intent i = new Intent(this, PickActivity.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
// Set the request code to any code you like, you can identify the
// callback via this code
startActivityForResult(i, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("returnKey1")) {
Toast.makeText(this, data.getExtras().getString("returnKey1"),
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_intentintent, menu);
return true;
}
,最后一个是第二个活动,PickActivity.java
public class PickActivity extends Activity{
Button submit;
EditText text1, text2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picklayout);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
text1 = (EditText) findViewById(R.id.input11);
text2 = (EditText) findViewById(R.id.input22);
text1.setText(value1);
text2.setText(value2);
}
submit = (Button)findViewById(R.id.button1);
/*
submit.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(this, text1.getText(), 2500).show();
}
});
*/
}
public void onClick(View v) {
Toast.makeText(this, text1.getText(), 2500).show();
finish();
}
@Override
public void finish() {
Intent data = new Intent(this, IntentintentActivity.class);
Toast.makeText(this, "finish state", 2500).show();
// Return some hard-coded values
data.putExtra("returnKey1", "Swinging on a star. ");
data.putExtra("returnKey2", "You could be better then you are. ");
setResult(RESULT_OK, data);
int result = 1;
//startActivityForResult(data, result);
super.finish();
}
}
我试图在第二个活动中为这个按钮设置监听器。
public class PickActivity extends Activity{
Button submit;
EditText text1, text2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picklayout);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
text1 = (EditText) findViewById(R.id.input11);
text2 = (EditText) findViewById(R.id.input22);
text1.setText(value1);
text2.setText(value2);
}
submit = (Button)findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(this, text1.getText(), 2500).show();
}
});
}
然而它说Toast.makeText(...)
不适用于参数(新的View.onClickListener)。
有人知道如何解决这个问题。谢谢。
答案 0 :(得分:1)
在你的Toast中你必须用getBaseContext()
替换'this'参数答案 1 :(得分:0)
你在内部类中,这应该是IntentintentActivity.this。
Toast.makeText(IntentintentActivity.this, data.getExtras().getString("returnKey1"),
Toast.LENGTH_SHORT).show();