【问题标题】:Input Text in an Alert Box在警报框中输入文本
【发布时间】:2013-03-05 22:38:20
【问题描述】:

我希望用户多次更改按钮的文本。为此,他长按该按钮。这是代码:

@Override
public void onCreate(Bundle savedInstanceState) {

//blah blah

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

    alert.setMessage("Nueva Categoria:");

    // Seting 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) {
        Button esteBoton = (Button) findViewById(R.id.button1);
        String newCateg = input.getText().toString();
        esteBoton.setText(newCateg);
      }
    });       


    Button button = (Button) findViewById(R.id.button1);
    button.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            alert.show();               
            return true;
        }
    });
}

好的。当我在 Eclipse 的设备模拟器中运行此代码时,如果这是我第一次在警报对话框中为按钮 1 输入文本,则没有问题,但如果我第二次尝试输入代码,应用程序将崩溃。我不是Java专家,但我认为这是由“输入”的“最终”属性引起的,一旦确定,我就无法更改它的值。 我该如何解决?代码很简单,我想保持这种方式。

【问题讨论】:

    标签: android android-alertdialog android-input-method


    【解决方案1】:

    尝试删除onCreate中的builder部分,并将其移动到onLongClickListener

    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.symptoms);
        button = (Button) findViewById(R.id.btDone);
    
        // final Dialog alert = builder.create();
    
        button.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
    
                // Declare your builder here - 
                final AlertDialog.Builder builder = new AlertDialog.Builder(
                        YOURACTIVITY.this);
                builder.setMessage("Nueva Categoria:");
                // Seting an EditText view to get user input
                final EditText input = new EditText(YOURACTIVITY.this);
                builder.setView(input);
                builder.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                String newCateg = input.getText().toString();
                                button.setText(newCateg);
                            }
                        });
    
                builder.show();
                return true;
            }
        });
    }
    

    试试这个,看看是否可行。

    【讨论】:

    • @Grishu 代码类似,谢谢大家。当我尝试你的建议时,Eclipse 在运行时吐出这个:E/AndroidRuntime(531): FATAL EXCEPTION: main java.lang.NullPointerException at com.android.internal.app.AlertController$AlertParams.(AlertController.java‌ ​:742) 在 android.view.View.performLongClick(View. java:2503) 在 android.view.View$CheckForLongPress.run(View.java:9056) 的 android.widget.TextView.performLongClick(TextView.java:7640)
    • 在 android.os.Handler.handleCallback(Handler.java:587) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java :123) 在 android.app.ActivityThread.main(ActivityThread.java:3683) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:507) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 在 dalvik.system.NativeStart.main(Native Method )
    • 对不起,你们的建议非常好。我在自己的代码中犯了一个错误。现在一切正常!谢谢。
    • 如果您还想格式化编辑文本,请看这里:stackoverflow.com/a/9345820/3904109
    【解决方案2】:

    试试下面的代码:

     public class MainActivity extends Activity {
               Button button;
               Context context;
         @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = MainActivity.this;
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                final AlertDialog.Builder alert = new AlertDialog.Builder(
                        context);
                alert.setMessage("Nueva Categoria:");
                // Seting an EditText view to get user input
                final EditText input = new EditText(context);
                alert.setView(input);
                alert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                String newCateg = input.getText().toString();
                                button.setText(newCateg);
                            }
                        });
                AlertDialog build = alert.create();
                build.show();
                return true;
            }
        });
     }
      }
    

    在按钮的 onLongClickListener 中定义您的对话框。查看代码,它现在工作得很好。

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多