0

手紙の例に従った。custom.xmlを介してカスタムボタンを使用しようとすると、エミュレータがJavaエラーでクラッシュします-クラスandroid.widget.Buttonを膨らませます。

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


public class MasterActivity extends Activity {

    Button uccBtn, tipBtn;

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

        uccBtn = (Button) findViewById(R.id.button1);
        tipBtn = (Button) findViewById(R.id.button2);
        uccBtn.setOnClickListener (uccApp);
        tipBtn.setOnClickListener (tipApp);

custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_pressed="true" android:drawable="@drawable/btn_ecolab_selected"></item>
<item android:state_focused="true"
 android:drawable="@drawable/btn_ecolab_highlight"></item>
<item android:drawable="@drawable/btn_ecolab"></item>
</Selector>

activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
android:background ="#ffffcc"
tools:context=".MasterActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:text="@string/button1" android:typeface="serif"
    android:background="@drawable/custom"
     />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/version"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/button10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="56dp"
    android:text="@string/button10" android:typeface="serif"
    android:background="@drawable/custom" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button10"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="59dp"
    android:text="@string/button2" android:typeface="serif"
    android:background="@drawable/custom"
     />

</RelativeLayout>

ボタンだけを名前(btn_ecolab)で参照すると、正常に機能し、押したり選択したりしたときにボタンのアニメーションが表示されません。Relitavelayoutが問題ではないことを確認するために、LinearLayoutで試してみました。カスタムボタンは私のRes/drawable-hpdiフォルダーにあります。また、-mdpi、-xhdpi、および-xxhdpiのボタンとcustom.xmlのコピーを試しました。@stringを割り当てずに試しました。

4

1 に答える 1

0

これを試してください。

uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (new OnClickListener() {              
                @Override
                public void onClick(View v) {
                    //your code is here
                }
            });

resフォルダーに1つのフォルダーを描画可能にし、custom.xmlを配置します

私はこの方法を使用しており、うまく機能しています。

于 2013-03-21T05:01:36.983 に答える