简体   繁体   English

Android:将NumberPicker中的整数传递给另一个活动

[英]Android: Pass an integer from NumberPicker to another activity

I am using the built in NumberPicker to have the user choose a number. 我正在使用内置的NumberPicker让用户选择一个号码。 Then the user will hit the "OK" button to confirm and the resulting action will open up another activity. 然后,用户将点击“确定”按钮进行确认,并且所产生的动作将打开另一个活动。 I want the number that the user chooses to be passed through to the other activity when the user hits the "OK" button. 我希望用户单击“确定”按钮时将用户选择的号码传递给其他活动。 I have done buttons that result in opening up a new activity and I know that the NumberPicker has a getValue() and you can pass stuff through with putExtra(), but I am not sure how to combine those with the onClick method. 我已经完成了导致打开新活动的按钮,而且我知道NumberPicker具有getValue(),并且可以通过putExtra()传递内容,但是我不确定如何将它们与onClick方法结合使用。 How do I go about doing this? 我该怎么做呢?

public class Source1 extends Activity implements View.OnClickListener {

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

@Override
public void onClick(View v) {
    int x = ((NumberPicker) v).getValue();
    Intent intent = new Intent(this, Destination.class);
    intent.putExtra("VarName", x);
    startActivity(intent);
}
}

This is the xml for my button: 这是我的按钮的xml:

<Button
    android:id="@+id/Id_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/numberPicker1"
    android:layout_centerHorizontal="true"
    android:onClick="methodName"
    android:text="@string/Ok" />

That was my attempt. 那是我的尝试。 Is this right/what needs to change? 这是正确的/什么需要改变? Do I even need the onClick in the button code since I am using the onClick method in the class? 因为我在类中使用onClick方法,我是否甚至需要按钮代码中的onClick?

Change your code as to get selected value from NumberPicker on Button click: 更改代码,以在单击按钮时从NumberPicker获取选定的值:

@Override
public void onClick(View v) {

    NumberPicker numPicker = (NumberPicker)findViewById(R.id.NumberPicker_id);
    int x = numPicker.getValue();
    Intent intent = new Intent(this, Destination.class);
    intent.putExtra("VarName", x);
    startActivity(intent);
}

Is this right/what needs to change? 这是正确的/什么需要改变?

The best way to test whether its right is: try to read VarName in Destination . 测试其是否正确的最佳方法是:尝试读取Destination VarName But see imran khan's answer. 但是请看伊姆兰·汗的答案。

Do I even the onClick in the button code since I am using the onClick method in the class? 由于我在类中使用onClick方法,我是否甚至在按钮代码中也包含onClick?

No, you should choose one or the other. 不,您应该选择其中一个。 Either use: 可以使用:

  1. onClick="methodName" in the XML, with public void methodName(View v) {} in Source1 XML中的onClick="methodName" ,在Source1带有public void methodName(View v) {}

  2. implement OnClickListener and use numberPicker.setOnClickListener(this); implement OnClickListener并使用numberPicker.setOnClickListener(this); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM