将代码移出sharedPreference需要协助

时间:2013-10-23 21:30:15

标签: android

我有一个使用共享首选项的alertDialog,因此对话框只能显示一次。你们中的一些人可能知道它导致我的活动表现得很有趣,我的意思是没有回应我的按钮的onClick。我被告知要移动alert.show();之后开始的活动的下半部分,超出if共享偏好的if (!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();
        }}

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

{{1}}

2 个答案:

答案 0 :(得分:0)

如果进一步阻止,您希望移动if (!initialDialogDisplayed)的结束括号,以便每次调用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();
    }}

答案 1 :(得分:0)

我对您当前遇到的问题或您想要的内容感到有些困惑,但如果您希望ButtonEditText代码无论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);