[英]I want numbers to be displayed in a text view from a dialog which appear when clicked on text view
public void goldWeight(){
Dialog dialog = new Dialog(HomeActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.gold_w_grm);
dialog.setTitle("GOLD WEIGHT[Grams]");
dialog.setCancelable(true);
NumberPicker np1=(NumberPicker)dialog.findViewById(R.id.grm);
np1.setMaxValue(9999);
np1.setMinValue(0000);
int grams = np1.getValue();
NumberPicker np2=(NumberPicker)dialog.findViewById(R.id.mgrm);
np2.setMaxValue(9999);
np2.setMinValue(0000);
int mgrams = np2.getValue();
double gw;
gw = grams+(mgrams/100f);
String value = "" + gw;
//value.toString();
//String numberAsString = Double.toString(gw);
TextView TV2 = (TextView)findViewById(R.id.UserGoldWeight);
TV2.setText(value);
//TV2.setText("hfhgf");
dialog.show();
} //Layout for this Dialog elativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GoldWeightGrams"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<NumberPicker
android:id="@+id/grm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_centerHorizontal="false"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/mgrm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_centerHorizontal="false"
android:layout_weight="1" />
Issue is program set 0.0 to text view when click on the view and fail to change it when dialog disappear. 问题是,在视图上单击时,程序将0.0设置为文本视图,而在对话框消失时无法更改它。 I want to get value from number pickers and show them in the text view when dialog disappear.
我想从数字选择器中获取价值,并在对话框消失时在文本视图中显示它们。
use following code i think your problem solve 使用以下代码我认为你的问题解决了
public void goldWeight() {
final Dialog dialog = new Dialog(HomeActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.testting);
dialog.setTitle("GOLD WEIGHT[Grams]");
dialog.setCancelable(true);
final NumberPicker np1 = (NumberPicker) dialog.findViewById(R.id.grm);
np1.setMaxValue(9999);
np1.setMinValue(0000);
final NumberPicker np2 = (NumberPicker) dialog.findViewById(R.id.mgrm);
np2.setMaxValue(9999);
np2.setMinValue(0000);
Button btnOK = (Button) dialog.findViewById(R.id.btnDialog);
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int grams = np1.getValue();
int mgrams = np2.getValue();
double gw = grams + (mgrams / 100f);
TextView TV2 = (TextView) findViewById(R.id.text);
TV2.setText("" + gw);
dialog.dismiss();
}
});
dialog.show();
}
change your gold_w_grm XML file Like: 更改您的gold_w_grm XML文件,例如:
<NumberPicker
android:id="@+id/grm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/mgrm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDialog"
android:text="OK"/>
its not changed because you put the calculation before the NumberPicker change. 它没有更改,因为您将计算放在NumberPicker更改之前。
Try to put event on them 尝试在他们身上放事件
int grams, mgrams;
double gw;
TextView TV2;
public void goldWeight(){
Dialog dialog = new Dialog(HomeActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.gold_w_grm);
dialog.setTitle("GOLD WEIGHT[Grams]");
dialog.setCancelable(true);
NumberPicker np1=(NumberPicker)dialog.findViewById(R.id.grm);
np1.setMaxValue(9999);
np1.setMinValue(0000);
//set the last value
np1.setValue(grams);
NumberPicker np2=(NumberPicker)dialog.findViewById(R.id.mgrm);
np2.setMaxValue(9999);
np2.setMinValue(0000);
//set the last value
np2.setValue(mgrams);
TV2 = (TextView)findViewById(R.id.UserGoldWeight);
np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
grams= numberPicker.getValue();
changeTextView();
}
});
np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
mgrams= numberPicker.getValue();
changeTextView();
}
});
dialog.show();
}
void changeTextView(){
// mgrams/100f -> its float and not able to do more than 3 decimal number
//gw = grams+(mgrams/100f);
gw = grams+(((double)mgrams)/10000);
//to be precise, convert it to decimal format (rounding), already tested and works perfectly on my device
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
String value = "" + df.format(gw);
TV2.setText(value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.