我有2个按钮和2个独立的活动,我想要控制流程如下:
Activityone
buttonOne
{➡{1}} ActivityTwo
{➡{1}} main.xml中:
buttonTwo
MainActivity.java
ActivityThree
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="right">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1.0">
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:layout_height="match_parent"
android:layout_width="50dp"
android:background="@drawable/rozmare"/>
<TextView
android:layout_height="match_parent"
android:text="کار های روزمره"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="25sp"/>
</LinearLayout>
<EditText
android:textSize="20.0sp"
android:gravity="top|right"
android:id="@+id/editText1"
android:padding="5.0dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text=""
android:capitalize="sentences"
android:layout_gravity="top|right"
android:textColor="#000000"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="65dp"
android:orientation="vertical"
android:layout_margin="1dp">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:layout_height="65dp"
android:layout_width="match_parent"
android:id="@+id/notelist"
android:background="@drawable/personal"
android:layout_margin="1dp"/>
<Button
android:layout_height="65dp"
android:layout_width="match_parent"
android:background="@drawable/kasbokar"
android:layout_margin="1dp"
android:id="@+id/notelist1"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
但是两个按钮都在执行相同的操作,两个按钮都将转到import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.widget.TextView;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button buttonOne;
Button buttonTwo;
EditText editText1;
String fileName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
getActionBar().hide();
getWindow().setSoftInputMode(3);
editText1 = (EditText) findViewById(R.id.editText1);
fileName = getResources().getString(R.string.file_name);
try {
FileInputStream fis = openFileInput(fileName);
byte[] readBytes = new byte[fis.available()];
fis.read(readBytes);
editText1.setText(new String(readBytes));
fis.close();
} catch (Exception e) {
return;
}
}
public void onPause() {
super.onPause();
try {
FileOutputStream fos = openFileOutput(fileName, 0);
fos.write(editText1.getText().toString().getBytes());
fos.close();
} catch (Exception e) {
return;
}
}
public void addListenerOnButton() {
final Context context = this;
buttonOne = (Button) findViewById(R.id.notelist);
buttonOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
buttonTwo = (Button) findViewById(R.id.notelist1);
buttonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
}
});}
public void EXIT(View view)
{
finish();
}}
,另一个问题是,如果我不接触<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.whitegate.noteking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NoteList"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteEdit"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteList1"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
<activity
android:name=".NoteEdit1"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified"/>
</application>
activityTwo
将不行。
答案 0 :(得分:2)
您已在第一个按钮的侦听器中添加了第二个按钮的侦听器,因此
如果我不接触ButtonOne,ButtonTwo将无效。
第二个按钮的监听器只有在按下第一个按钮时才会初始化
但它们应该是分开的
public void addListenerOnButton() {
final Context context = this;
buttonOne = (Button) findViewById(R.id.notelist);
buttonOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
}
});
buttonTwo = (Button) findViewById(R.id.notelist1);
buttonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
}
他们两个都要活动两个
此行为与简单地使用相同的布局结构有关,因此请使用settext
函数或在XML代码中进行更改。
答案 1 :(得分:0)
您的问题的解决方案是在xml中定义onClick函数
<Button
android:text="Don't have an account? Join!"
android:onClick="toRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
然后在班级中定义你的功能
public void toRegister(View view){
Intent newIntent = new Intent(this, SignUpActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LoginActivity.this.startActivity(newIntent);
}
答案 2 :(得分:0)
您已在第一个侦听器中添加了第二个侦听器。
buttonOne.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList.class);
startActivity(intent);
buttonTwo = (Button) findViewById(R.id.notelist1);
// >>>>>>>>>>>>>>>>>>>>>>> inside buttonOne
buttonTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, NoteList1.class);
startActivity(intent);
}
});
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}
}); // >>>>>>>>>> button one ends here
声明onClickListener
的另一种简单方法是将其声明为对象字段,如下所示
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.notelist1: {
// go to notelist 1
break;
}
case R.id.notelist: {
// go to notelist
break;
}
}
}
};
并在下面的按钮上设置监听器
buttonOne.setOnClickListener(clickListener);
buttonTwo.setOnClickListener(clickListener);