我对第二项活动为什么不开放感到困惑?

时间:2019-10-23 18:25:06

标签: android android-studio

我一直在尝试打开第二个活动,即“ SecondActivity.java”,但无济于事。它应该能够显示用户输入的值并在第二个活动中显示它。但是该按钮似乎根本不起作用。

public class MainActivity extends AppCompatActivity {

    public Spinner spnCurrency1, spnCurrency2;
    public Button btnSubmit;
    public Button btnDifferent;
    public EditText from;
    public TextView to;

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

        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnDifferent = (Button) findViewById(R.id.btnDifferent);
        from = (EditText) findViewById(R.id.InputEditText);
        to = (TextView) findViewById(R.id.OutputTextView);


        spnCurrency1 = (Spinner) findViewById(R.id.spnCurrency1);
        List<String> lstCurrency1 = new ArrayList<String>();
        lstCurrency1.add("Euro");
        lstCurrency1.add("USD");
        lstCurrency1.add("Pound");
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lstCurrency1);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnCurrency1.setAdapter(dataAdapter1);


        spnCurrency2 = (Spinner) findViewById(R.id.spnCurrency2);
        List<String> lstCurrency2 = new ArrayList<String>();
        lstCurrency2.add("Euro");
        lstCurrency2.add("USD");
        lstCurrency2.add("Pound");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lstCurrency2);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnCurrency2.setAdapter(dataAdapter2);
    }

    public void onClick(View v) {
        int index1 = spnCurrency1.getSelectedItemPosition();
        int index2 = spnCurrency2.getSelectedItemPosition();
        float value = Float.parseFloat(from.getText().toString());

        float ratio[] = {0.9f, 1.0f, 0.78f};
        float result = value / ratio[index1] * ratio[index2];

        switch (v.getId()) {
            case R.id.btnSubmit:
                to.setText(result + "");
                break;
            case R.id.btnDifferent:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("Result", result);
                startActivity(intent);
                break;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您的代码中有两个问题。

  1. View.OnClickListener应用于Activity
  2. onClick中将btnDifferent侦听器设置为onCreate()。像btnDifferent.setOnClickListener(this)。或者,如果要从XML设置侦听器,请确保已为btnDifferent
  3. 添加了相同的代码

答案 1 :(得分:0)

  1. Invalid MEX-file 'C:\C++ForMatlab\test.mexw64': Gateway function is missing 进行活动
  2. 使用
  3. 之类的View.OnClickListener方法将onClick侦听器设置为btnDifferent
onCreate()

答案 2 :(得分:0)

选项-1:您必须实现View.OnClickListener

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnDifferent = (Button) findViewById(R.id.btnDifferent);

        btnSubmit.setOnClickListener(this);
        btnDifferent.setOnClickListener(this);

        ...
    }

    @Override
    public void onClick(View v) {
        int index1 = spnCurrency1.getSelectedItemPosition();
        int index2 = spnCurrency2.getSelectedItemPosition();
        float value = Float.parseFloat(from.getText().toString());

        float ratio[] = {0.9f, 1.0f, 0.78f};
        float result = value / ratio[index1] * ratio[index2];

        switch (v.getId()) {
            case R.id.btnSubmit:
                to.setText(result + "");
                break;
            case R.id.btnDifferent:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("Result", result);
                startActivity(intent);
                break;
        }
    }
}

选项-2:您必须调用具有适当视图的onClick

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnDifferent = (Button) findViewById(R.id.btnDifferent);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick(v);
        }
    });

    btnDifferent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick(v);
        }
    });

    ...
}

选项-3:如果从xml调用onClick

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/android_list_view_tutorial_with_example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--android:onClick="onClick" should be same-->
    <Button
        android:id="@+id/btnDifferent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Button Different" />

    <!--android:onClick="onClick" should be same-->
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Button Submit" />
</LinearLayout>