我正在开发一个应用程序,它将执行以下操作:
- 允许第一个用户“Sam”在 editText 字段中键入他/她的消息。
- 按屏幕底部的“Alex”按钮启动新活动ActivityAlex
- 允许用户“Alex”编辑 editText 字段。
- 按屏幕底部的“Sam”按钮重新启动活动 MainActivity
到目前为止,我认为我已经完成了大部分工作,但我一直在坚持如何修复程序,以便它可以完成我想要它做的事情。
这是我的日志猫
这是我的主要活动:
package com.chatfriends;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button alexButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
alexButton = (Button) findViewById(R.id.button1); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
alexButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
alexbutton(arg0);
}
public void alexbutton(View v) {
Intent intent = new Intent(MainActivity.this, ActivityAlex.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
这是我的活动亚历克斯:
package com.chatfriends;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityAlex extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button samButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
samButton = (Button) findViewById(R.id.button2); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
samButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
samButton(arg0);
}
public void samButton(View v) {
Intent intent = new Intent(ActivityAlex.this, MainActivity.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
这是我的清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chatfriends"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>
<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=".ActivityAlex" />
</application>
</manifest>
这是 XML 布局文件。
活动主:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button1"
android:onClick="startalexbutton" />
</RelativeLayout>
activityex.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".ActivityAlex" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button2"
android:onClick="startMainActivity" />
</RelativeLayout>