点击不点火?

时间:2013-12-14 04:20:41

标签: java android bluetooth

我是Android新手,无法完成一些简单的事情!

以下是一些基本代码:

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Lets go ahead and set our buttons up.
    ServerButton = (Button)this.findViewById(R.id.ServerButton);
    ClientButton = (Button)this.findViewById(R.id.ClientButton);
    BTServer = new BluetoothServerService(this, mHandler);      
}

@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;
}
    public void ServerButtonClick(View v){
    Log.d("BLUETOOTH MAIN ACTIVITY", "It got here");
    BTServer.start();
}

private final Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        switch (msg.what){
        case HANDLER_CHANGE_SERVER_STATUS:
            switch(msg.arg1){
            case SERVER_STATUS_OFF:
                CURRENT_SERVER_STATUS = SERVER_STATUS_OFF;
                //We should probably add some logging, and the change to the button
                break;
            case SERVER_STATUS_ON:
                CURRENT_SERVER_STATUS = SERVER_STATUS_ON;
                //Again lets add some logging, etc.
                break;
            case SERVER_STATUS_ON_CONNECTED:
                CURRENT_SERVER_STATUS = SERVER_STATUS_ON_CONNECTED;
                //Again lets add some logging, etc.
                break;
            }
        }
        return;
    }
};

我的XML看起来像这样:     

<Button
    android:id="@+id/ServerButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="146dp"
    android:layout_marginLeft="64dp"
    android:text="Connect to Server" />

<Button
    android:id="@+id/ClientButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:text="Start Server" 
    android:onClick="ServerButtonClick"/>

在我的日志中,我看到日志说它创建了BTServer对象,但是当我点击按钮时,我无法显示任何日志。

有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

onCreate方法

中定义 ClientButton 的点击操作
 ClientButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

ServerButton 也定义了上面的点击操作

 ServerButton.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     // Perform action on click
                 }
             });

答案 1 :(得分:1)

对我来说,我发现最好让你的Activity实现android.view.View.OnClickListener,然后将你的Button的onClickListener设置为你的Activity。

以下是Activity类的示例:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity implements OnClickListener
{
    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);

        setContentView (R.layout.activity_main);

        Button serverButton = (Button)this.findViewById (R.id.ServerButton);
        serverButton.setOnClickListener (this);

        Button clientButton = (Button)this.findViewById (R.id.ClientButton);
        clientButton.setOnClickListener (this);
    }

    @Override
    public void onClick(View button)
    {
        int buttonId = button.getId ();

        if (buttonId == R.id.ServerButton)
        {
            // Do server stuff.
        }

        if (buttonId == R.id.ClientButton)
        {
            // Do client stuff.
        }
    }
}

答案 2 :(得分:0)

在Android中定义android:onClick =“someMethod”属性时,它只为你实现OnClickListener。

因此,您有责任定义onCLickListener方法并从中调用ServerButtonClick方法。

在Activity OnCreate方法中添加此方法。它会起作用。

ServerButton.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
            ServerButtonClick();  
          }
       });

此外, android:onClick方法仅适用于API级别&gt; 4以后。所以如果你的目标是最低SDK版本&lt; 1.6请不要使用