2017-07-09 9 views
0

カスタムビュー(LinearLayoutを拡張)を初期化して追加の引数を渡すことはできますか? 特定の情報を表示または非表示にするブール値を設定したいとします。私は、ビューを初期化するときに、ユーザーが設定することを希望ブールvarialbe shouldShowTextがあるCustomButtonクラス内 customButton = findViewById(R.id.customButton);カスタムイニシャライザでカスタムビューを初期化する(追加データ)

は今、私はこのような何かを持っています。 私のカスタムボタン(LinearLayout)の中には、Context、Context、AtributeSet、Context、AttributeSet、intの3つのコンストラクタがあります。どこでもinitを呼びますが、追加データを渡す方法はわかりません。

答えて

1

あなたは、このようなあなたのXMLでカスタムレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<com.example.CustomLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:id="@+id/customButton" 
    android:layout_height="match_parent" 
    > 

</com.example.CustomLayout> 

あなたを追加することができますし、Javaコードであなたは

CustomLayout customButton = (CustomLayout)findViewById(R.id.customButton); 
// and you can call your method like this. 
customButton.shouldShowText([your boolean]); 

が編集、次のように呼び出すことができます。 カスタムクラス

public class CustomButton extends LinearLayout { 
    public void shouldShowText(boolean showText) { 
     // Do your action here 
    } 
} 
+0

hmmですが、これには新しい方法を作成する必要がありますか?何かが好きです: 'void shouldShowText(boolean should){...}'?今私は変数を持っています: 'public class CustomButton extends LinearLayout {public boolean shouldShowText ...}' – codddeer123

+0

私の答えを –

+0

に更新しました。これはまさに私が探していたものではありません。もう1つの質問 - このユーザーがこの機能を実装する方法を教えてください。抽象クラスを作成する必要がありますか? – codddeer123

関連する問題