简体   繁体   English

如何从 android java 中的警报框中获取文本视图中的编辑文本值?

[英]How do I get the edit text value in text view from an alert box in android java?

I want to get some integer value from the user he/she click on the an alert box will show and asks for enter the total budget.我想从用户那里获得一些 integer 值,他/她单击将显示的警报框并要求输入总预算。 When he/she puts the total budget in the alert box field then it'll be show in text view in the same activity.当他/她将总预算放入警报框字段时,它将显示在同一活动的文本视图中。

Each dialog has a positive button, and that positive button has a callback where you can get the text from edit text, like Edittext.getText and display it for textview like TextView.setText .每个对话框都有一个肯定按钮,并且该肯定按钮有一个回调,您可以在其中从编辑文本中获取文本,例如Edittext.getText并为 textview 显示它,例如TextView.setText

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // your work here
        textview.setText = Edittext.getText

            dialog.dismiss();
        }
    });
alertDialog.show();

Do refer to topic regarding custom dialog box: How to create a Custom Dialog box in android?请参考有关自定义对话框的主题: 如何在 android 中创建自定义对话框?

You can create a custom dialog box that have a custom layout R.layout.budget_dialog :您可以创建具有自定义布局R.layout.budget_dialog的自定义对话框:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#ffffffff">


  <EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/edit_budget"
    android:hint="Enter budget"
    android:layout_below="@+id/a"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="20dp"
    android:textSize="18sp"
    android:textColor="#ff000000"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="OK"
    android:id="@+id/btn_dialog"
    android:gravity="center_vertical|center_horizontal"
    android:layout_below="@+id/text_dialog"
    android:layout_marginBottom="20dp"
    android:background="@drawable/btn_flat_red_selector"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff" />

</RelativeLayout>

And then obtain the value by:然后通过以下方式获取值:

public void showDialog(Activity activity, String msg){
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.budget_dialog);

    final EditText edtBudget = dialog.findViewById(R.id.edit_budget);
    Button dialogButton = dialog.findViewById(R.id.btn_dialog);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get budget string value
            String budget = edtBudget.getText().toString();
            //Do something with your value
            dialog.dismiss();
        }
    });

    dialog.show();

}

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

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