gpt4 book ai didi

Android Number Picker 获取 textview 的值(value)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:45 26 4
gpt4 key购买 nike

我正在使用带有数字选择器的警报对话框,但我不知道在按下警报的“确定”按钮时将数字选择器值获取到 Activity 中的 TextView 的简单方法。

Activity java代码:

public class SecondActivity extends Activity {

public void button2 (View v){

View v1 = getLayoutInflater().inflate(R.layout.dialog1, null);

NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);
picker.setMinValue(1);
picker.setMaxValue(20);
picker.setWrapSelectorWheel(false);
//final int number = picker.getValue();

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView (v1);
builder.setTitle("Cantidad");
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });

AlertDialog dialog = builder.create();
dialog.show();
}

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

}

}

显示布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<NumberPicker
android:id="@+id/np1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

Activity 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ale.demarco.ale005.SecondActivity$PlaceholderFragment" >

<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Text2"
android:textSize="20sp"/>



<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button2"
android:onClick="button2"
android:layout_gravity="right" />


</LinearLayout>

最佳答案

使用 picker.getValue() 获取选择器的当前值。

Dialog 按钮被选中时,用选择的值更新 Activity 中的 TextView

 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
int pickedValue = picker.getValue();
// set your TextView id instead of R.id.textView1
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(Integer.toString(pickedValue));
return;
} });

你也可以像这样添加一个监听器来监听数字的变化

picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// do your other stuff depends on the new value

}
});

关于Android Number Picker 获取 textview 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23554346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com