This article describes the use of button buttons and dynamically adding components in Android development. Share it for your reference, as follows:
package .lab2; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { EditText et1; //Enter the EditText of the username EditText et2; //EditText for entering password ImageButton ib; //Declare ImageButton Button btn; //Reset button Context context; LinearLayout mLinearLayout; //This LineatLayout is used to place added components TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); init(); //initialization //Click imagebutton OnClickListener listener = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(().toString().equals("android") && ().toString().equals("2015")) { (.state1); //Setting to change the original picture (); (); } else //When entering the account or password incorrectly { (""); ("Error account or password"); } } }; (listener); //Long press imagebutton OnLongClickListener longlistener = new OnLongClickListener() { @Override public boolean onLongClick(View arg0) { // TODO Auto-generated method stub (context, "The TextView has been added dynamically", Toast.LENGTH_SHORT); tv = new TextView(context); ("Here is a dynamically added TextView"); (tv); //Add components return false; } }; (longlistener); //Reset OnClickListener listener2 = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub (.state2); ("Please enter a username"); ("Please enter your password"); (""); (""); (); //Position the cursor EditText (); (); (); //When clicking the reset button, the original added components will be cleared together } }; (listener2); } //Initialize the function public void init() { et1 = (EditText)findViewById(.et_username); et2 = (EditText)findViewById(.et_passward); ib = (ImageButton)findViewById(); btn = (Button)findViewById(); context = ; mLinearLayout = (LinearLayout)findViewById(); } }
xml:
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android: android:hint="Please enter a username" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <EditText android: android:hint="Please enter your password" android:inputType="textPassword" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ImageButton android: android:src="@drawable/state2" android:layout_width="50dp" android:layout_height="50dp"/> <Button android: android:text="Reset" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> </LinearLayout>
For more information about Android related content, please check out the topic of this site:Summary of the usage of basic Android components》、《Android development introduction and advanced tutorial》、《Android layout layout tips summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.