从其他活动调用对话方法

时间:2017-02-26 16:23:34

标签: java android sharedpreferences

我有问题,我不能从另一个活动调用对话框方法,该对话框将用户输入到共享预防 在这里,我想要检查如果不存在共享首选项,我想要检查对话框是否显示获取用户输入,然后点击确定后继续运行主要活动 这里主要是

public class main extends Activity {


private Button button;
private EditText CardNumber;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    checkid();



        }
    });
}
public void checkid(){
 File f = new File(
            "/data/data/com.example.ahmed_samir.enjaz/shared_prefs/MyPrefs.xml");
if (f.exists())
    Toast.makeText(OpreatorMobily.this, "exist:", Toast.LENGTH_LONG).show();
else {


  NationalId na= new NationalId();
    na.dialogmethod();

}

}
}

这是我的第二个对话活动

public class NationalId extends Activity {

public static final String MyPREFERENCES = "MyPrefs";
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;

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

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_national_id);
    dialogmethod();
    // components from main.xml
    button = (Button) findViewById(R.id.button1);
    result = (TextView) findViewById(R.id.tv1);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

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

        @Override
        public void onClick(View arg0) {

            dialogmethod();


        }

    });

}


public   void  dialogmethod(){
    LayoutInflater li = LayoutInflater.from(context);
    View promptsView = li.inflate(R.layout.prompts, null);

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

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

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

    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String n = userInput.getText().toString();
                            SharedPreferences.Editor editor = sharedpreferences.edit();
                            editor.putString(Name, n);
                            editor.commit();
                            // edit text
                            result.setText(userInput.getText());

                            Toast.makeText(NationalId.this, "saved:" + n, Toast.LENGTH_LONG).show();



                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

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

    // show it
    alertDialog.show();

}

}

1 个答案:

答案 0 :(得分:0)

你不应该这样做。一种可能的解决方案是创建一个单独的类,其中包含对话框的静态方法。

因此,在您的活动中,您可以这样称呼它:

./chrome --no-sandbox http://cp7.awardspace.com/speed-test/awardspace-data1mb.zip

静态方法内部:

./chrome --no-sandbox --headless http://cp7.awardspace.com/speed-test/awardspace-data1mb.zip