使用edittext的Alertdialog构建器

时间:2013-10-17 23:11:12

标签: android alertdialog

我正在使用“警报”对话框来编辑textview小部件。当对话框弹出时,edittext可以用用户文本填充吗?我看到有自定义图块,图标和按钮的位置,但没有输入消息。 alterdialog是用onlcicklistener触发的,它从文本视图中获取文本。

        @Override
public void onClick(View item) {

    switch (item.getId()) {
    case R.id.textView1:
        strEditText = Title1.getText().toString();
        Log.d("TAG", strEditText);
        strReturn = SetDialogBox(strEditText);
        Title1.setText(strReturn);

        break;

Altertdialog

        private String SetDialogBox(String strEditText) {

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Title");
    // alert.setMessage("Message");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);

    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable value = input.getText();

        //  Log.d("TAG", Editable);

        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    alert.show();
    return strReturn;

}

已解决的文本编辑在最终EditText输入后添加了行= new EditText(this);

            input.setText(strEditText);

1 个答案:

答案 0 :(得分:1)

这应该适用于editText

方法

private void showChooseSuitDialog() {
        final Dialog chooseSuitDialog = new Dialog(myContext);
        chooseSuitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        chooseSuitDialog.setContentView(R.layout.choose_suit_dialog);
        final Spinner suitSpinner = (Spinner) chooseSuitDialog.findViewById(R.id.suitSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                myContext, R.array.suits, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        suitSpinner.setAdapter(adapter);
        Button okButton = (Button) chooseSuitDialog.findViewById(R.id.okButton);
        okButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                validSuit = (suitSpinner.getSelectedItemPosition()+1)*100;
                String suitText = "";
                if (validSuit == 100) {
                    suitText = "Diamonds";
                } else if (validSuit == 200) {
                    suitText = "Clubs";
                } else if (validSuit == 300) {
                    suitText = "Hearts";
                } else if (validSuit == 400) {
                    suitText = "Spades";
                }
                chooseSuitDialog.dismiss();
                Toast.makeText(myContext, "You chose " + suitText, Toast.LENGTH_SHORT).show();
                myTurn = false;
                makeComputerPlay();

            }
        }); 
        chooseSuitDialog.show();
    }

choose_suit_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/chooseSuitLayout"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/chooseSuitText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose a suit."
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:textColor="#FFFFFF"
>
</TextView>
<Spinner 
android:id="@+id/suitSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
<Button
android:id="@+id/okButton"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="OK"
>
</Button>
</LinearLayout>