我使用内置的NumberPicker让用户选择一个数字.然后用户将点击"确定"按钮进行确认,结果操作将打开另一个活动.当用户点击"确定"按钮时,我想要用户选择的号码传递给另一个活动.我已经完成了打开新活动的按钮,我知道NumberPicker有一个getValue(),你可以通过putExtra()传递东西,但我不知道如何将它们与onClick方法结合起来.我该怎么做呢?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的按钮的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" />
Run Code Online (Sandbox Code Playgroud)
那是我的尝试.这是对的/需要改变的吗?我是否甚至需要按钮代码中的onClick,因为我在类中使用onClick方法?
更改您的代码以从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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2104 次 |
| 最近记录: |