1

ボタンがあります。そのボタンをクリックすると、ダイアログボックスが開きます。そのダイアログボックスの1つのEditTextと[OK]ボタンに、テキストが表示されているときに[OK]ボタンをクリックすると、テキストが入力されます。
どうすればいいのかわからない、助けて

4

6 に答える 6

3

以下を使用してください

 import android.app.Activity;
 import android.app.AlertDialog;
  import android.content.Context;
      import android.content.DialogInterface;
   import android.os.Bundle;
    import android.view.LayoutInflater;
 import android.view.View;
    import android.view.View.OnClickListener;
   import android.widget.Button;
    import android.widget.EditText;

 public class MainActivity extends Activity {

final Context context = this;
private Button button;
private EditText result;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // components from main.xml
    button = (Button) findViewById(R.id.buttonPrompt);
    result = (EditText) findViewById(R.id.editTextResult);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View view= li.inflate(R.layout.prompts, null);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(view);

            final EditText userInput = (EditText) view
                    .findViewById(R.id.editTextDialogUserInput);

            // set dialog message
            alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                    // get user input and set it to result
                    // edit text
                    result.setText(userInput.getText());
                    }
                  })
                .setNegativeButton("Cancel",
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                    dialog.cancel();
                    }
                  });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }
    });
}

}

于 2013-03-25T07:44:36.137 に答える
2

これがお役に立てば幸いです

 AlertDialog.Builder alert = new AlertDialog.Builder(con);
                final EditText input = new EditText(con);
                String s="your text";

                alert.setView(input);
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString().trim();
                        Toast.makeText(con, value, Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
                alert.show();
于 2013-03-25T07:46:26.010 に答える
1

(ここにある他の回答よりも)多くのことを学ぶのに役立つと思うので、できるだけ少ないコードで手順を説明します。

  • ダイアログボックスを起動するために必要なボタンにOnClickListenerを設定します
  • ダイアログボックスのカスタムビューをXMLで作成し、custom_box.xmlと呼びます。
  • その後、OnClickメソッドで、以下を使用してそのカスタムxmlをダイアログに設定します。
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_box);
  • アクティビティの場合と同じようにJavaでボタンとEditTextを接続しますが、アクティビティのようにthis.findViewById(...)の代わりにdialog.findViewById(R.id ...)を使用するようになりました。
  • ダイアログボックス内のボタンにOnClickListenerを設定し、そこからdismiss andchangetextアクションを呼び出します。

必要に応じて、かなり優れたチュートリアルへのリンクもあります。Androidのドキュメントもあります。

于 2013-03-25T07:49:16.550 に答える
1
public class MainActivity extends Activity {

final Context context = this;
private Button button;
private TextView result;
private TextView result2;

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 // components from main.xml
 button = (Button) findViewById(R.id.button1);
 result = (TextView) findViewById(R.id.textView1);
 result2 = (TextView) findViewById(R.id.textView2);

 // add button listener
 button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {

       // get prompts.xml view
       LayoutInflater li = LayoutInflater.from(context);
       View view= li.inflate(R.layout.dailog, null);

       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
               context);

       // set prompts.xml to alertdialog builder
       alertDialogBuilder.setView(view);

       final EditText userInput = (EditText) view
               .findViewById(R.id.edit1);
       final EditText userInput2 = (EditText) view
               .findViewById(R.id.edit2);

       // set dialog message
       alertDialogBuilder
           .setCancelable(false)
           .setPositiveButton("OK",
             new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog,int id) {
               // get user input and set it to result
               // edit text
               result.setText(userInput.getText());
               result2.setText(userInput2.getText());
               }
             }) 
           .setNegativeButton("Cancel",
             new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog,int id) {
               dialog.cancel();
               }
             });

       // create alert dialog
       AlertDialog alertDialog = alertDialogBuilder.create();

       // show it
       alertDialog.show();

   }
 });
}

}

これは私の正確な答えです

于 2013-03-25T08:41:59.390 に答える
0

ここにAndroidでカスタムダイアログを作成する方法を説明するリンクがありますこれはあなたを助けます

于 2013-03-25T07:46:02.300 に答える
0
Call ForgetPswPopUp() method name


 public void ForgetPswPopUp() {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        HomeActivity.this);
                alertDialog.setTitle("set Title");
                final EditText input = new EditText(HomeActivity.this);
                input.setHint("Textbox hint");
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, position);
                input.setLayoutParams(lp);
                alertDialog.setView(input);
                alertDialog.setNegativeButton("Button name",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                alertDialog.show();
            }
于 2014-12-02T05:30:58.783 に答える