6

私は痛々しいほどアンドロイドに不慣れで、壁にぶつかりました。

線形レイアウトをボタンのように機能させ、長押しと長押しのアクションを変えようとしています。その理由は、各「ボタン」に2つの異なる形式のテキストラベルを付けることができるからです。次のようなもの:

-------------------------
|          2nd          |  <- Label for long press (regular/smaller type)
|           =           |  <- Label for regular press (bold/larger type)
-------------------------

私が見つけた投稿は、線形レイアウトで通常のクリックを受け取る方法を説明しています(私はレイアウトXMLでonClick属性を使用しています)。しかし、私は長押しで運がなかった。Aleksander Gralakの回答で説明されているように、xmlの新しいonLongClick属性を定義しようとしました:android:onClickのように、XMLレイアウトでの長押しの定義。しかし、そのような運はありませんでした-それはテキストビューを対象としていたようです、私はそれを線形レイアウトに変更しようとしましたが、惨めに失敗しました。

問題のオブジェクトは次のとおりです。Main.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/darkgrey_button"
    android:onClick="equals" android:longClickable="true" android:id="equalsbutton"
    android:focusableInTouchMode="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2nd"
        android:id="@+id/textView"
        android:duplicateParentState="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" = "
        android:id="@+id/textView1"
        android:duplicateParentState="true"/>
</LinearLayout>

Main.java

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

public void equals(View view) {
    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
4

1 に答える 1

7

をレイアウトに追加idします。

<LinearLayout
    android:id="@+id/my_button_layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    … >

あなたのMain.java

あなたへの参照を取得し、LinearLayoutを設定しますOnLongClickListener

LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.my_button_layout);
…
buttonLayout.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
          Toast.makeText(Main.this, "Long click!", Toast.LENGTH_SHORT).show();
          return true;
    }

});
于 2013-03-28T07:01:07.003 に答える