-1

クリックを受け入れないように見える「saveStats」ボタンがあります。

エミュレーターでは、クリックのアニメーションはありません。

Eclipseをクリーンアップして再起動しようとしましたが、うまくいきませんでした。

ボタンだけが機能しない、正常に機能するCheckBoxウィジェットもあります。ここに私のコードがあります:

助けてくれてありがとう:)

Button saveStats;

CheckBox ageCheck, heightCheck, weightCheck, fatCheck;


protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_personal_data);

        saveStats = (Button) findViewById(R.id.saveStats);

        ageCheck = (CheckBox) findViewById(R.id.checkBoxAge);
        heightCheck = (CheckBox) findViewById(R.id.checkBoxHeight);
        weightCheck = (CheckBox) findViewById(R.id.checkBoxWeight);     
        fatCheck = (CheckBox) findViewById(R.id.checkBoxFat);

        saveStats.setOnClickListener(this);
        ageCheck.setOnClickListener(this);
        heightCheck.setOnClickListener(this);
        weightCheck.setOnClickListener(this);
        fatCheck.setOnClickListener(this);

}

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


    switch(v.getId()){

    case R.id.saveStats:
    {
         Toast.makeText(this, "working", Toast.LENGTH_LONG).show();
          return;
        }
    case R.id.checkBoxAge:
    {
        if(ageCheck.isChecked())
            Toast.makeText(this, "ok", Toast.LENGTH_LONG).show()
            return;
    }
////
////
////
////....

ボタンの XML コード:

 <Button
        android:id="@+id/saveStats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="save" />
4

2 に答える 2

1

OnClickListener()このようにして実装する必要があります

public class ActivityName extends Activity implements OnClickListener
{

それからoverrideあなたのonClick()

@Override
public void onClick(View v)
{
    ...
}

ただのメモ。これを行う別の方法は、xml で同じクリック関数を宣言し、Buttonその関数を Java コードに記述することです。これにより、コード内で設定implements OnClickListenerを宣言する必要がなくなります。どちらの方法で行うかは、何が一番好きかによって異なりますが、別のオプションにすぎません。以下に少し例を示します。ButtonsonClickListener()

Button関数を宣言するたびにxmlで

<Button
   android:id="@+id/btn1"
    ...
    android:onClick="functionName"/>
<Button
   android:id="@+id/btn2"
    ...
    android:onClick="functionName"/>

次に、Javaコードで:

public void functionName(View v)   
{
    ... switch on your id just like you would the other way
}

メソッドは であり、 aをその唯一の としてpublic受け入れ、あなたが宣言したものと同じ名前である必要がありますViewparamxml

于 2013-05-14T15:00:38.707 に答える
1

break ステートメントを使用していません。クラスが OnClickListener を実装していることを確認してください

@Override
public void onClick(View v) {

    switch(v.getId())
    {
    case R.id.saveStats :
        Toast.makeText(ActivityName.this, "Button clicked", Toast.LENGTH_LONG).show();
        break;
    case R.id.checkBoxAge :
                         //dosomething
        break;
    }

}

トーストを表示するには、アクティビティ コンテキストを使用します。理由を知るには、以下のリンクのコモンズウェアによる回答を確認してください

アクティビティ コンテキストまたはアプリケーション コンテキストをいつ呼び出すか?

activity_main.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"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginBottom="66dp"
    android:layout_marginRight="35dp"
    android:text="Button1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:text="Button2" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="18dp"
    android:text="CheckBox" />
 </RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

CheckBox cb ; 
Button b,b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b= (Button) findViewById(R.id.button1);
    b1= (Button) findViewById(R.id.button2);
    cb = (CheckBox) findViewById(R.id.checkBox1);
    cb.setOnClickListener(this);
    b.setOnClickListener(this);
    b1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
     case R.id.button1 :
            Toast.makeText(MainActivity.this, "Button clicked1", Toast.LENGTH_LONG).show();
            break;
     case R.id.button2 :
          Toast.makeText(MainActivity.this, "Button clicked2", Toast.LENGTH_LONG).show();
            break;
     case R.id.checkBox1:
         if(cb.isChecked())
          Toast.makeText(MainActivity.this, "check box clicked", Toast.LENGTH_LONG).show();
            break;

    }       
}
}
于 2013-05-14T15:00:50.607 に答える