简体   繁体   English

“接线”按钮可更改android中的视图

[英]“Wiring-up” Buttons to change View in android

Im building an application that requires when you press the button IE (search) then it opens the search page, click the (home) button then it goes back to the home view. 我正在构建一个需要按IE(搜索)按钮然后打开搜索页面,单击(主页)按钮然后返回到主页视图的应用程序。 what I'm missing is the knowledge to make that connection between those two views. 我所缺少的是在这两个视图之间建立联系的知识。 this is what the home page looks like in XML. 这就是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 " />

//Legal Button //法律按钮

<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" />

some pictures of the application. 该应用程序的一些图片。 http://imgur.com/aVpqUCZ http://imgur.com/aVpqUCZ

Button searchStudentButton;
Button newStudentButton;
Button legalButton;

searchStudentButton = (Button) findViewById(R.id.button1);

searchStudentButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
    CurrentActivity.this.startActivity(myIntent);
    }
});

newStudentButton = (Button) findViewById(R.id.button2);

newStudentButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
    CurrentActivity.this.startActivity(myIntent);
    }
});

legalButton = (Button) findViewById(R.id.button3);

legalButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
    CurrentActivity.this.startActivity(myIntent);
    }
});

Don't forget to add your new activity in the AndroidManifest.xml: 不要忘记在AndroidManifest.xml中添加新活动:

<activity android:label="@string/app_name" android:name="searchStudentActivity"/>
<activity android:label="@string/app_name" android:name="newStudentActivity"/>
<activity android:label="@string/app_name" android:name="legalActivity"/>

When dealing with multiple views and/or buttons like you are, I usually prefer using only one instance of onClickListener for all the views, to keep the code cleaner. 当像您一样处理多个视图和/或按钮时,我通常更喜欢对所有视图仅使用一个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;
            }
        }
    }
}

I would recommend that you change the android:id attribute of your buttons to a more meaningful name. 我建议您将按钮的android:id属性更改为更有意义的名称。 This makes it easier to see what you're referencing in the code. 这使得查看代码中的引用变得更加容易。 I personally prefer prepending my views with an abreviation of the view class such as btn_ for Button and tv_ for TextView . 我个人更喜欢在视图前面添加一个视图类的缩写,例如btn_表示Buttontv_表示TextView Remember to update your calls to findViewById() and the id's used in the switch statement. 记住要更新对findViewById()调用以及在switch语句中使用的ID。

Finally, don't forget to add your activities to the AndroidManifest.xml file as Sagar Pilkhwal posted, if you haven't already. 最后,不要忘记按照Sagar Pilkhwal的说明将活动添加到AndroidManifest.xml文件中(如果尚未添加)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM