[英]Android Studio: On button click go to previous activity (not the back button)
我必須創建兩個活動頁面。 在第一個中,用戶輸入他們的名字,該名字被傳遞給第二個活動。 一切正常,但是當按下第二個按鈕“button_do”時,應用程序需要退出,當按下第一個“button_dont”時,它需要 go 到前一個活動以重新輸入名稱。 另外一點,我需要將 editText go 的結果放入 sharedPreferance,只要它還不存在……這兩個方面都有問題。 說明還說按下的按鈕的值需要為 0 或 1。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="4"
android:layout_column="3"
android:layout_weight="1"
android:layout_gravity="fill"
>
<TextView
android:id="@+id/textView"
android:layout_width="256dp"
android:layout_height="66dp"
android:layout_row="0"
android:layout_column="0"
android:text="@string/enterName"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="441dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:textSize="40sp"/>
<Button
android:id="@+id/next"
android:layout_width="127dp"
android:layout_height="64dp"
android:layout_row="2"
android:layout_column="0"
android:text="@string/next" />
</GridLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NameActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="416dp"
android:layout_height="87dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_dont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_call"
android:layout_margin="5sp"/>
<Button
android:id="@+id/button_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thank_you"
android:layout_margin="5sp"/>
</LinearLayout>
</LinearLayout>
package com.example.androidlabs;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button next;
EditText editText;
public static final int REQ_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = findViewById(R.id.next);
EditText editText = findViewById(R.id.editText);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),NameActivity.class);
String name = editText.getText().toString();
i.putExtra("Value",name);
startActivityForResult(i,REQ_CODE);
}
});
}
@Override
protected void onPause() {
Object fileName;
SharedPreferences prefs = getSharedPreferences("name.txt", Context.MODE_PRIVATE);
super.onPause();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(REQ_CODE==1){
if(resultCode == REQ_CODE){
int result = data.getIntExtra("result",0);
if(result==0){
editText.setText("");
} if(result==1){
finish();
}
}
}
}
}
package com.example.androidlabs;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class NameActivity extends AppCompatActivity {
public static final int REQ_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
TextView welcome = findViewById(R.id.welcome);
Intent i = getIntent();
String name = i.getStringExtra("Value");
welcome.setText("Welcome "+ name +"!");
Button button_dont = findViewById(R.id.button_dont);
Button button_do = findViewById(R.id.button_do);
button_dont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result=0;
Intent resultIntent = new Intent();
resultIntent.putExtra("result",result);
setResult(REQ_CODE, resultIntent);
finishActivity(REQ_CODE);
}
});
button_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result=1;
Intent resultIntent = new Intent();
resultIntent.putExtra("result",result);
setResult(REQ_CODE, resultIntent);
finish();
}
});
}
}
我不會為 button_dont 使用 onClickListener onClickListener()
。 而是使用這樣的東西
public void onButtonDo_Clicked(View caller){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
確保將您在 XML 文件中使用的按鈕的 OnClick 設置為此方法。 這樣,每當您按下按鈕時,您都會返回到上一個 Activity。 它應該是這樣的:
<Button
android:id="@+id/button_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonDo_Clicked"
android:text="@string/thank_you"
android:layout_margin="5sp"/>
對於 button_do,我不知道您為什么希望程序關閉。 最好不要實現這個,只使用android接口。
對於第二個問題,您可以使用 prefs.contains(name) 檢查該鍵的條目是否已經存在。
只需從活動中刪除finish()
。
因為當你去第二個活動並完成第一個活動時,沒有活動和堆棧。
因此,如果您從第二個活動單擊后退按鈕,如果堆棧中沒有活動,則應用程序將完成。
你應該使用這種方法。
前任。
在 Activity.java
Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.
在 ActivityB.Java 中
只需單擊內置的后退按鈕。
或者如果您想使用自己的后退按鈕。
使用finish();
在按鈕單擊事件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.