Android开发链接XML按钮到Java [英] Android development Linking XML button to Java

查看:118
本文介绍了Android开发链接XML按钮到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发,我基本上是试图建立一个非常基本的应用程序,这将使用户能够在他们的无线网络开关。

I am new to android development, I am basically trying to create a very basic app which will enable the user to switch on their wifi.

我的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
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:layout_marginTop="16dp"
    android:gravity="top"
    android:text="@string/welcome"
    android:textSize="25sp" />

<Button
    android:id="@+id/wifi_on"
    android:layout_width="150dp"
    android:layout_height="60dp"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="112dp"
    android:text="@string/wifi_button" />

`

我的Java文件:

包com.example.dosomething;

进口android.os.Bundle; 进口android.app.Activity; 进口android.view.Menu; 进口android.view.View; 进口android.view.View.OnClickListener; 进口android.widget.Button;

公共类MainActivity扩展活动实现OnClickListener {

protected Button wifi_on;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


}

public Button getWifiOn(){


    return wifi_on;

} 


public void setWifiOn(Button on){

    wifi_on = on;


}

}`

我的问题是我怎么可以参考该按钮的XML文件中到java文件,以进一步告诉它,如果被点击的按钮,应开启无线网络。谢谢你。

My question is how can I refer to the button inside the XML file onto the java file to further tell it that if the button is clicked on it should turn on the wifi. Thanks.

推荐答案

有这样做的几种方法。

There are several ways of doing this.

  1. 你可以做的就是这行添加到您的&LT;按钮在XML标记

安卓的onClick =setWifiOn

那么该函数的参数更改为

then change the parameter of that function to

public void getWifiOn(View v){


return wifi_on;

} 

这样,你不需要的onClick 或任何监听器

2。你可以做,如果你想要所有的按钮共享相同的功能,然后给他们像

2.You can do something similar if you want all Buttons to share the same function then give them all the function name like

android:onClick="someFunction"

然后在Java中做这样的事情

then in Java do something like

public void someFunction(View v)
{
    Button btn = (Button)v;  
    switch (v.getId())
    {
       case (R.id.wifi_on:
        setWifiOn(btn);
        break;
       case (R.id.differentBtnId):
       // do some other things
       break;
     }
}

}

3.Less在许多情况下,有吸引力的,恕我直言

3.Less attractive in many situations, IMHO

    public class MainActivity extends Activity implements OnClickListener {
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button wifiBtn = (Button) findViewById(R.id.wifi_on);
    wifiBtn.setOnClickListener(this);  // your onClick below will be called then you will still have to check the id of the Button
    // multiple buttons can set the same listener and use a switch like above

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

注意 3号是唯一一个在您需要实现OnClickListener

Note number 3 is the only one in which you need implements OnClickListener

按钮文档

我离开了另一种方式,因为我认为它最丑的,如果你有一个以上的按钮

I left out the other way because I think its the ugliest if you have more than one Button

这篇关于Android开发链接XML按钮到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆