延长按键的Andr​​oid,XML布局按键、布局、oid、Andr

2023-09-07 12:16:59 作者:春风吹皱了露水

我有以下类(包括到另一个类)

I have the following class (included into another class)

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

按钮的显示用下列code制备:

The display of the button is made using the following code:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    setContentView(ll);
}

我如何定义按键布局到.xml文件,而不是在java code做的?

How can I define the Button layout into the .xml file instead of doing it in the java code?

我已经试过了:

<AudioRecordTest.test.RecordButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:id="@+id/record" />   

但它不工作...

But it is not working...

非常感谢,

约阿希姆

推荐答案

我理解(列入其他类)作为你有一个内部类 RecordButton

I understand "(included into another class)" as you have an inner class RecordButton.

假设你的包是 AudioRecordTest.test (这将是一个非常糟糕的选择名称)和您RecordButton类是内部类AudioRecord.class的,你需要使用

Assuming your package is AudioRecordTest.test (which would be a very bad name choice) and your RecordButton class is an inner class of AudioRecord.class, you need to use:

 <view class="AudioRecordTest.test.AudioRecord$RecordButton"

使用 $ 符号来分隔内部类。你需要写引号内的限定名称。此外,请确保您创建类公共静态,否则将不可见的。

Use the $ sign to separate inner classes. You need to write the qualified name inside quotes. Also, make sure you create your class public static, or it won't be visible.

BTW:?什么特别的原因,你把它创建为一个内部类,而不必把它分割

BTW: any particular reason you create it as an inner class instead of having it separate?