A simple example of button usage
activity_main,xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:showDividers="middle"
android:dividerPadding="10dp"
tools:context="com.example.xxxx.myapplication.MainActivity" >
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
android:text="1" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:hint="1" />
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
android:text="2 "/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2 " />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="S" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="R" />
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp" />
</LinearLayout>
mainActivity.java
package com.example.xxxx.myapplication;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//TextView txt =findViewById(R.id.show_msg);
private Button button1,button2;
TextView txt;
//TextView txt2 =findViewById(R.id.txt2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txt=(TextView)this.findViewById(R.id.txt);
//BUTTON1 111
this.button1 = (Button) this.findViewById(R.id.bt1);
this.button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText et1=(EditText)findViewById(R.id.et1);
String s1 = et1.getText().toString();
txt.setText("@11111"+s1);
}
});
//BUTTON2 222
this.button2 = (Button) this.findViewById(R.id.bt2);
this.button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText et2=(EditText)findViewById(R.id.et2);
String s2 = et2.getText().toString();
txt.setText("@22222"+s1);
}
});
}
}
网友评论