Android自訂群組件

來源:互聯網
上載者:User

標籤:i++   .net   get   str   res   edittext   prot   content   載入   

[參考的原文地址]

http://blog.csdn.net/l1028386804/article/details/47101387


實現方式:

一:自訂一個含有EditText和Button的組件

先建立一個buttonext_layout的自訂群組件的XML布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <EditText        android:id="@+id/editText"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:inputType="text"/>    <Button        android:id="@+id/clearButton"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:text="Clear"/></LinearLayout>

二:建立自訂控制項類ButExt 

public class ButExt extends LinearLayout {    private EditText mEditText;    private Button mButton;    public ButExt(Context context) {        super(context);        //使用布局資源填充視圖        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        //載入布局檔案        mInflater.inflate(R.layout.buttonext_layout, this, true);        mEditText = (EditText) findViewById(R.id.editText);        mButton = (Button) findViewById(R.id.clearButton);        hookupButton(context);    }    /**     * button處理常式     */    private void hookupButton(final Context context) {        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mEditText.setText("");                Toast.makeText(context, "文字框資料已清除", Toast.LENGTH_SHORT).show();            }        });    }   /**
*給EditText賦值
*/
    public void setText(String txt){
mEditText.setText(txt);
}
}

三:MainActivity類的實現

public class MainActivity extends AppCompatActivity {    private LinearLayout mLinearLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //動態添加我們自訂的組件        mLinearLayout = (LinearLayout) findViewById(R.id.main_layout);        for (int i = 0; i < 3; i++) {            imageViewExt editText = new imageViewExt(this);
editText.setText(i+"); mLinearLayout.addView(editText); } }}

 


Android自訂群組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.