Android Studio:单击按钮转到上一个活动(不是后退按钮)
我必须创建两个活动页面。第一个用户输入其名称,该名称传递给第二个活动。这一切都在起作用,但是当按下第二个按钮“ button_do”时,应用程序需要退出,当第一个“ button_dont”需要退出时,它需要转到上一个活动才能重新输入名称。同样,作为另一点,我需要使EditText的结果进入共享限制,只要它尚未存在……这两个方面都有问题。指示还说按钮的值必须为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();
}
});
}
}
I have to create two activity pages. In the first the user enters their name, which is passed to the second activity. That is all working, but when the second button is pressed "button_do" the app needs to exit, when the first "button_dont" it needs go to the previous activity to re enter the name. Also as another point I need to have the result of the editText go into a sharedPreferance as long as it is not already there... having problems with both aspects. Also the instructions say that the value of the button pressed needs to be 0 or 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();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会为button_dont使用
onclicklistener()
。而是使用类似的内容,请确保将在XML文件中使用的按钮的单击设置为此方法。这样,只要按下按钮,就可以返回上一个活动。
看起来应该这样:
对于Button_do,我不知道为什么您要该程序关闭。最好不要实现此目标,而只为此使用Android界面。
对于第二个问题,您可以使用prefs.contains(name)检查该密钥退出的条目是否已经出现。
I wouldn't use an
onClickListener()
for the button_dont. but instead use something like thisMake sure you set the OnClick of the button you use in the XML file to this method. In this way whenever you press the button, you go back to the previous Activity.
it should look like this:
For the button_do I don't know why you would want the program to shut off. Its better to dont implement this and just use the android interface for this.
For the second problem, you can use prefs.contains(name) to check if an entry for that key exits already.