0

我有一个使用共享首选项的 alertDialog,因此该对话框只能显示一次。你们中的一些人可能知道它导致我的活动表现得很有趣,我的意思是没有响应我的按钮的 onClick。有人告诉我将我的活动的下半部分从 之后开始的共享偏好声明中alert.show();移出我遇到的问题是我不知道将我的活动的下半部分移到哪里。不知道该不该搬ifif (!initialDialogDisplayed) {

final EditText et = (EditText) findViewById(R.id.editText1);
  Button getAnswer = (Button) findViewById(R.id.button1);
            getAnswer.setOnClickListener(new OnClickListener() {

以上共享偏好或什么。所以我希望有人能真正帮助我解决这个问题。

   public class MainActivity extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
    if (!initialDialogDisplayed) {
        Editor editor = preferences.edit();
        editor.putBoolean("InitialDialog", true);
        editor.commit();


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

        alertDialog.setTitle("Alert");
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setMessage("Dialog");

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
         });

        alertDialog.show();
                final EditText et = (EditText) findViewById(R.id.editText1);
                Button getAnswer = (Button) findViewById(R.id.button1);
                getAnswer.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {       
                        if (et.getText().toString().length()==0) {
                            Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                        }else{
                            EditText et = (EditText) findViewById(R.id.editText1);
                            String searchTerm = et.getText().toString().trim();         
                            Intent in = new Intent(MainActivity.this, ListView.class);
                            in.putExtra("TAG_SEARCH", searchTerm);
                            startActivity(in);
                        }

                    }
                });
            }
         }


        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }}
4

2 回答 2

0

您希望将if (!initialDialogDisplayed)if 块的结束括号进一步向上移动,以便OnClickListener每次onCreate调用按钮时都设置按钮,而不仅仅是如果!initialDialogDisplayed

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
if (!initialDialogDisplayed) {
    Editor editor = preferences.edit();
    editor.putBoolean("InitialDialog", true);
    editor.commit();


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

    alertDialog.setTitle("Alert");
    alertDialog.setIcon(R.drawable.ic_launcher);
    alertDialog.setMessage("Dialog");

    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

        }
     });

    alertDialog.show();


    }  //XXX finish the if block here, so that our OnClickListener is set


            final EditText et = (EditText) findViewById(R.id.editText1);
            Button getAnswer = (Button) findViewById(R.id.button1);
            getAnswer.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {       
                    if (et.getText().toString().length()==0) {
                        Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                    }else{
                        EditText et = (EditText) findViewById(R.id.editText1);
                        String searchTerm = et.getText().toString().trim();         
                        Intent in = new Intent(MainActivity.this, ListView.class);
                        in.putExtra("TAG_SEARCH", searchTerm);
                        startActivity(in);
                    }

                }
            });
        // }  // don't finish if block here
     }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }}
于 2013-10-23T22:04:36.483 回答
0

我对您当前遇到的问题或您想要的内容有些困惑,但是如果您希望ButtonandEditText代码运行而不管 的值如何,请initialDialogDisplayed移动此代码

 final EditText et = (EditText) findViewById(R.id.editText1);
            Button getAnswer = (Button) findViewById(R.id.button1);
            getAnswer.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {       
                    if (et.getText().toString().length()==0) {
                        Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                    }else{
                        EditText et = (EditText) findViewById(R.id.editText1);
                        String searchTerm = et.getText().toString().trim();         
                        Intent in = new Intent(MainActivity.this, ListView.class);
                        in.putExtra("TAG_SEARCH", searchTerm);
                        startActivity(in);
                    }

                }
            });

直属

setContentView(R.layout.activity_main);
于 2013-10-23T22:05:10.280 回答