我无法通过单击按钮

时间:2016-09-20 13:27:40

标签: android android-layout

我正在尝试通过两个文本框发送MainActivity数据,只需点击一下按钮即可将数据发送到SecondActivity,这两个数字应显示在另一个下方。

SecondActivity要么显示第一个或第二个文本框的数据,要么不显示两者。

我在这里做错了什么?

MainActivity代码:

public class MainActivity extends Activity {
    public final static String MESSAGE_KEY="experiment.com.anew.madhu.assignment.message_key";
    public final static String MESSAGE_KEY2="experiment.com.anew.madhu.assignment.message_key2";

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

    public void compute(View v) {

        EditText e1 = (EditText) findViewById(R.id.editText);
        EditText e2 = (EditText) findViewById(R.id.editText2);

        String message= e1.getText().toString();
        String message2= e2.getText().toString();

        Intent intent=new Intent (this,SecondActivity.class);
        intent.putExtra(MESSAGE_KEY,message);
        intent.putExtra(MESSAGE_KEY2,message2);
        startActivity(intent);
    }
}

来自第二项活动:

public class SecondActivity extends Activity {
    public final static String MESSAGE_KEY="experiment.com.anew.madhu.assignment.message_key";
    public final static String MESSAGE_KEY2="experiment.com.anew.madhu.assignment.message_key2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        String message = intent.getStringExtra(MESSAGE_KEY);
        String message2 = intent.getStringExtra(MESSAGE_KEY2);

        TextView textView=new TextView(this);
        textView.setText(message);
        setContentView(textView);

        TextView textView2=new TextView(this);
        textView2.setText(message2);
        setContentView(textView2);

        setContentView(R.layout.second_layout);
    }
}

我已尝试将第二项活动更改为LinearLayout,从AppCompatActivity更改为Activity,将MainActivity文本框ID的相同ID添加到TextView,尝试过不同的ID也完全删除了TextView,但仍未获得两个数字的输出。

2 个答案:

答案 0 :(得分:0)

请参阅以下代码:

second_layout

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

SecondActivity

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);

    TextView tvFirst = (TextView) findViewById(R.id.textView1);
    TextView tvScnd = (TextView) findViewById(R.id.textView2);

    Intent intent=getIntent();
    String message = intent.getStringExtra(MESSAGE_KEY);
    String message2 = intent.getStringExtra(MESSAGE_KEY2);

    tvFirst.setText(message);
    tvScnd.setText(message2);
}

答案 1 :(得分:0)

MainActivity:

public class MainActivity extends Activity {
public final static String MESSAGE_KEY="experiment.com.anew.madhu.assignment.message_key";
public final static String MESSAGE_KEY2="experiment.com.anew.madhu.assignment.message_key2";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    compute(); //view initialization must be on onCreate
}

public void compute(){

    EditText e1 = (EditText) findViewById(R.id.editText);
    EditText e2 = (EditText) findViewById(R.id.editText2);

    String message= e1.getText().toString();
    String message2= e2.getText().toString();

    Intent intent=new Intent (this,SecondActivity.class);
    intent.putExtra(MESSAGE_KEY,message);
    intent.putExtra(MESSAGE_KEY2,message2);
    startActivity(intent);

   }
}

SecondActivity:

public class SecondActivity extends Activity {
public final static String MESSAGE_KEY="experiment.com.anew.madhu.assignment.message_key";
public final static String MESSAGE_KEY2="experiment.com.anew.madhu.assignment.message_key2";

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

    Intent intent=getIntent();
    String message = intent.getStringExtra(MESSAGE_KEY);
    String message2 = intent.getStringExtra(MESSAGE_KEY2);

    TextView textview = (TextView) findViewById(R.id.textView);
    TextView textview2 = (TextView) findViewById(R.id.textView2);
    textView.setText(message);
    textView2.setText(message2)
   }
}