在警报框中输入文本

时间:2013-03-05 04:37:58

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

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

@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的设备模拟器中运行此代码时,如果它是第一次在Alert Dialog中为按钮1输入文本时没有问题,但是如果我第二次尝试输入代码,则应用程序崩溃。我不是Java的专家,但我认为这是由于“输入”的“最终”属性,我不能在确定之后改变它的值。 我该如何解决?代码很简单,我希望以这种方式保留它。

2 个答案:

答案 0 :(得分:1)

尝试删除onCreate中的构建器部件并将其移动到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;
        }
    });
}

试试这个,看看是否有效。

答案 1 :(得分:0)

尝试以下代码:

 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中定义对话框。查看代码,它的工作真棒。