標籤: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自訂群組件