当mesg框退出时,它返回到prevoius激活状态
我从堆栈溢出获得代码以调出一个mesg盒子。
当我在ActGetKey中调用一个mesga盒时,它会在按下退出按钮后返回到起始活动MainActivity。
mesg框代码:
public void msbox(String str,String str2){AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setTitle(STR); dlgAlert.setMessage(STR2); dlgAlert.setPositiveButton(“OK”,new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int whichButton){finish();}}); dlgAlert.setCancelable(真); dlgAlert.create()表示(); }
MainActivity的代码public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
keyDB db;
db = new keyDB(this);
db.setUp();
cPassKey mPassKey = new cPassKey("mysec","mypublic","myphase");
db.updateExchange("ted2", mPassKey);
Cursor cursor =db.selectKey("ted2");
// get keys in current pos of database
cPassKey mret=db.loadRecord(cursor);
String name;
Cursor test =db.selectKey("ted1");
name = test.getString(2);
name = test.getString(3);
cursor = db.selectRecords();
int id; String ted;
if (cursor != null)
{
do {
id = cursor.getInt(0);
ted = cursor.getString(1);
} while (cursor.moveToNext());
cursor.close();
}
}
public void sendMessage(View view) {
// Intent intent = new Intent(this,ActGetKey.class); Intent intent = new Intent(this,ActGetKey.class); startActivity(意向); }
}
ActGetKey活动的代码public class ActGetKey extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String selItem=null;
long d=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_get_key);
keyDB db;
db= new keyDB(this);
Cursor cursor=db.selectRecords();
String ted="na";
int id=0;
if (cursor != null) {
// get keys in current pos of database
cPassKey mPassKey=db.loadRecord(cursor);
// displkay the keys on app
loadKeys(mPassKey);
// fill up spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
if (cursor != null) {
do {
id = cursor.getInt(0);
ted = cursor.getString(1);
categories.add(ted);
} while (cursor.moveToNext());
cursor.close();
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// set selected item
spinner.setSelection(0);
}
}
public void Update3(View view)
{d ++; EditText temp;
temp = findViewById(R.id.publicKey);
String strPublicKey=(String)temp.getText().toString();
if (isValidString(strPublicKey))
{
msbox( getString(R.string.badtext), "Text for publick key is invalid");
return;
}
temp = findViewById(R.id.publicKey);
String strPrivetKey=(String)temp.getText().toString();
temp = findViewById(R.id.publicKey);
String strKeyPhrase=(String)temp.getText().toString();
// check for bad dada
}
boolean isValidString( String test)
{
if (test==null)
return false;
return true;
}
public void Update2(View view) {
// put keys in keyclass
msbox("kkk","kjjh");
if (true)
return;
keyDB db;
db= new keyDB(this);
cPassKey mPassKey = getKeys();
db.updateExchange(selItem, mPassKey);
// pus keyclass in data base
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
selItem = parent.getItemAtPosition(position).toString();
keyDB db;
db= new keyDB(this);
Cursor cursor =db.selectKey(selItem);
// get keys in current pos of database
cPassKey mPassKey=db.loadRecord(cursor);
// displkay the keys on app
loadKeys(mPassKey);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
boolean loadKeys(cPassKey mPassKey)
{
EditText temp;
temp = findViewById(R.id.publicKey);
temp.setText(mPassKey.publickKey);
temp = findViewById(R.id.secretKey);
temp.setText(mPassKey.secretKey);
temp = findViewById(R.id.phraseKey);
temp.setText(mPassKey.phraseKey);
return true;
}
public void msbox(String str,String str2)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle(str);
dlgAlert.setMessage(str2);
dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
cPassKey getKeys()
{
EditText temp;
temp = findViewById(R.id.publicKey);
String strPublicKey= temp.getText().toString();
temp = findViewById(R.id.secretKey);
String StrSecretKey= temp.getText().toString();
temp = findViewById(R.id.phraseKey);
String StrPhraseKey= temp.getText().toString();
cPassKey mPassKey = new
cPassKey( StrSecretKey,strPublicKey, StrPhraseKey);
return mPassKey;
}
}
公共类ActGetKey扩展AppCompatActivity实现AdapterView.OnItemSelectedListener {
String selItem=null;
long d=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_get_key);
keyDB db;
db= new keyDB(this);
Cursor cursor=db.selectRecords();
String ted="na";
int id=0;
if (cursor != null) {
// get keys in current pos of database
cPassKey mPassKey=db.loadRecord(cursor);
// displkay the keys on app
loadKeys(mPassKey);
// fill up spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
if (cursor != null) {
do {
id = cursor.getInt(0);
ted = cursor.getString(1);
categories.add(ted);
} while (cursor.moveToNext());
cursor.close();
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// set selected item
spinner.setSelection(0);
}
}
public void Update3(View view)
{d ++; EditText temp;
temp = findViewById(R.id.publicKey);
String strPublicKey=(String)temp.getText().toString();
if (isValidString(strPublicKey))
{
msbox( getString(R.string.badtext), "Text for publick key is invalid");
return;
}
temp = findViewById(R.id.publicKey);
String strPrivetKey=(String)temp.getText().toString();
temp = findViewById(R.id.publicKey);
String strKeyPhrase=(String)temp.getText().toString();
// check for bad dada
}
boolean isValidString( String test)
{
if (test==null)
return false;
return true;
}
public void Update2(View view) {
// put keys in keyclass
msbox("kkk","kjjh");
if (true)
return;
keyDB db;
db= new keyDB(this);
cPassKey mPassKey = getKeys();
db.updateExchange(selItem, mPassKey);
// pus keyclass in data base
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
selItem = parent.getItemAtPosition(position).toString();
keyDB db;
db= new keyDB(this);
Cursor cursor =db.selectKey(selItem);
// get keys in current pos of database
cPassKey mPassKey=db.loadRecord(cursor);
// displkay the keys on app
loadKeys(mPassKey);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
boolean loadKeys(cPassKey mPassKey)
{
EditText temp;
temp = findViewById(R.id.publicKey);
temp.setText(mPassKey.publickKey);
temp = findViewById(R.id.secretKey);
temp.setText(mPassKey.secretKey);
temp = findViewById(R.id.phraseKey);
temp.setText(mPassKey.phraseKey);
return true;
}
public void msbox(String str,String str2)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle(str);
dlgAlert.setMessage(str2);
dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
cPassKey getKeys()
{
EditText temp;
temp = findViewById(R.id.publicKey);
String strPublicKey= temp.getText().toString();
temp = findViewById(R.id.secretKey);
String StrSecretKey= temp.getText().toString();
temp = findViewById(R.id.phraseKey);
String StrPhraseKey= temp.getText().toString();
cPassKey mPassKey = new
cPassKey( StrSecretKey,strPublicKey, StrPhraseKey);
return mPassKey;
}
}
这是因为onClickListener
中的finish()
方法。 Finish()
不取消对话框,它完成活动。 如果您试图隐藏对话框,请使用dismiss()