Button
程序员文章站
2022-03-31 08:36:21
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:layout_marginLeft="36dp"
android:text="用户名" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_toRightOf="@id/textView"
android:layout_alignBottom="@id/textView"
android:layout_alignBaseline="@id/textView"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_alignLeft="@id/textView"
android:layout_marginTop="36dp"
android:text="密 码" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextTextPersonName"
android:layout_alignLeft="@id/editTextTextPersonName"
android:layout_alignBottom="@id/textView2"
android:ems="10"
android:inputType="numberPassword"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button2"
android:layout_alignBottom="@id/button2"
android:text="确定" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextTextPersonName2"
android:layout_alignRight="@id/editTextTextPersonName2"
android:text="充填" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button2"
android:layout_alignRight="@id/button2"
android:onClick="onClick_Event"
android:text="取消" />
</RelativeLayout>
package com.example.my;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button confirmBtn,resetBtn;
private EditText ed1,ed2;
private String name="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editTextTextPersonName);
ed2=(EditText)findViewById(R.id.editTextTextPersonName2);
confirmBtn=(Button)findViewById(R.id.button);
//第一种方式
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
name=ed1.getText().toString();
Toast.makeText(MainActivity.this,name+",您的登录信息以提交",Toast.LENGTH_SHORT).show();
}
});
//第二种方式
resetBtn=(Button)findViewById(R.id.button2);
Button.OnClickListener listener=new Button.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"请重置信息",Toast.LENGTH_SHORT).show();
}
};
resetBtn.setOnClickListener(listener);
}
//第三种方式
public void onClick_Event(View view){
Toast.makeText(MainActivity.this,"您取消了已填写的信息",Toast.LENGTH_SHORT).show();
}
}
本文地址:https://blog.csdn.net/SeNiLS/article/details/109368175
上一篇: Ubuntu 16 配置 adb