这个android代码看起来很好,但没有在模拟器上运行

时间:2015-11-04 13:31:21

标签: android android-intent

我创建这个只是为了理解意图的作用以及它们是如何做的:

第一项活动:

public class FirstActivity extends AppCompatActivity {

  private Button launch;
  private EditText label;
  private String msg;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    launch=(Button)findViewById(R.id.launch);
    label=(EditText)findViewById(R.id.et);
    msg= label.getText().toString();

    launch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent intent=SecondActivity.newIntent(FirstActivity.this, msg);
            startActivity(intent);
        }
    });
  }
}

第二项活动:

public class SecondActivity extends AppCompatActivity {

  private EditText et1;
  private static String EXTRA ="extra";
  private static String message="msg";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    et1=(EditText)findViewById(R.id.et1);

    message=getIntent().getStringExtra(message);
    et1.setText(message);
  }

  public static Intent newIntent(Context packageContext, String msg) {
    Intent i = new Intent(packageContext, SecondActivity.class);
    i.putExtra(EXTRA, msg);
    return i;
  }
}

这些是两个活动的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="horizontal">

    <EditText
        android:text="First Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        android:inputType="text"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Launch the second activity"
        android:id="@+id/launch"/>

</LinearLayout>

第二项活动:

<?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">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Activity"
        android:id="@+id/et1"/>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

你应该以常规的方式做,不要创造任何方法。

在您的第一项活动中:

        @Override
        public void onClick(View v) {
          Intent intent = new Intent(this, SecondActivity.class);
          startActivity(intent);
        }

它应该有用。

您可以通过以下方式为您的意图添加内容(就像您尝试使用msg一样):

intent.putExtra("Value1", "This value one for ActivityTwo ");
intent.putExtra("Value2", "This value two ActivityTwo"); 

然后在活动2 中检索数据如下:

Bundle extras = getIntent().getExtras();
if (extras == null) {
  return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
  // do something with the data
} 

答案 1 :(得分:0)

 @Override
    public void onClick(View v) {     
  Intent intent=SecondActivity.newIntent(FirstActivity.this, targetActivity.class);
        startActivity(intent);
}

答案 2 :(得分:0)

    public class FirstActivity extends AppCompatActivity {

private Button launch;
private EditText label;
private String msg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

launch=(Button)findViewById(R.id.launch);
label=(EditText)findViewById(R.id.et);


launch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     msg= label.getText().toString();
      Intent intent=SecondActivity.newIntent(FirstActivity.this, SecondActivity.class);
      intent.putExtra("msg",msg);
        startActivity(intent);
    }
 });
 }
 }