2

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

        @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;

替代对话框

        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);
4

1 回答 1

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();
    }

选择套装对话框.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>
于 2013-10-17T23:46:01.360 回答