首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在android中切换视图的"Wiring-up“按钮

在android中切换视图的"Wiring-up“按钮
EN

Stack Overflow用户
提问于 2014-09-06 03:50:07
回答 2查看 184关注 0票数 1

我正在建立一个应用程序,这需要当你按下按钮,即(搜索),然后它打开搜索页面,单击(主页)按钮,然后它返回到主页视图。我缺少的是在这两个视图之间建立联系的知识。这就是主页在XML中的样子。

代码语言:javascript
运行
复制
<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 " />

//法律按钮

代码语言:javascript
运行
复制
<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

EN

Stack Overflow用户

发布于 2014-09-06 06:54:24

在处理像您这样的多个视图和/或按钮时,我通常倾向于对所有视图只使用一个onClickListener实例,以保持代码的整洁。

代码语言:javascript
运行
复制
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属性更改为更有意义的名称。这使您更容易看到您在代码中引用的内容。我个人更喜欢在视图前面加上视图类的缩写,比如用于Buttonbtn_和用于TextViewtv_。记住要更新对findViewById()的调用和switch语句中使用的id。

最后,如果您尚未将活动添加到Sagar Pilkhwal posted中,请不要忘记将其添加到AndroidManifest.xml文件中。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25693027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档