简体   繁体   English

我希望通过单击文本视图时出现的对话框在文本视图中显示数字

[英]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.

相关问题 我想要如图所示的轮廓文本字段,未单击edittext或微调器我想要与按钮单击上的视图相同 - I want outlined text field with as shown in picture not clicked on edittext or spinner i want same as it is view on Button click 当我在其他活动上单击文本视图时,我想显示该特定文本视图的内容。 一世 - I want to display the contents of that specific text view when i click on the text view on a different activity. I 我想用文本视图创建一个计数器 - i want to create a counter with a text view 解析日期,该日期是rss feed中的字符串,然后显示在文本视图中。 - Parsing a date which is a string from an rss feed and then displayed in a text view. Java文本对话框,单击了哪个按钮? - java text dialog, which button was clicked? 对话框中的android文本视图 - android text view in dialog box 单击按钮时如何显示文本 - how to make text appear when a button is clicked 我想制作短信应用程序我有列表视图,其中包含给我发短信的人数但姓名重复 - i want to make sms app I have list view that have the numbers of people who text me but the names is repeated 当我单击活动时,我在其中有3个文本视图。 我想2秒的差距后加载文本视图一个又一个什么样的? - When I click on the activity I have 3 Text View in that. I want to load the Text View one after another like after a gap of 2 seconds? 我将视图膨胀了 5 次,膨胀的视图有一个 EditText,现在我在屏幕上有 5 个 EditText,如何从这些 EditTexts 中获取文本 - I inflated a view 5 times, the view which was inflated had a EditText, now i have 5 EditText on the screen, how to get the text from those EditTexts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM