我正在建立一个应用程序,这需要当你按下按钮,即(搜索),然后它打开搜索页面,单击(主页)按钮,然后它返回到主页视图。我缺少的是在这两个视图之间建立联系的知识。这就是主页在XML中的样子。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//Title
//Search Student Button
<Button
android:id="@+id/button1"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Search Student" />
//New Student Button
<Button
android:id="@+id/button2"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="New Studetn " />//法律按钮
<Button
android:id="@+id/button3"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Legal Info" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Student Registration "
android:textAppearance="?android:attr/textAppearanceLarge" />
应用程序的一些图片。http://imgur.com/aVpqUCZ
发布于 2014-09-06 06:54:24
在处理像您这样的多个视图和/或按钮时,我通常倾向于对所有视图只使用一个onClickListener实例,以保持代码的整洁。
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
}我建议您将按钮的android:id属性更改为更有意义的名称。这使您更容易看到您在代码中引用的内容。我个人更喜欢在视图前面加上视图类的缩写,比如用于Button的btn_和用于TextView的tv_。记住要更新对findViewById()的调用和switch语句中使用的id。
最后,如果您尚未将活动添加到Sagar Pilkhwal posted中,请不要忘记将其添加到AndroidManifest.xml文件中。
https://stackoverflow.com/questions/25693027
复制相似问题