第三单元控件
Button控件
知识点一:如何设置点击事件
(1)方法一:
首先对xml中想要设置点击的控件添加onclick属性
即android onclick=“点击后要进行的函数名”
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
/>
</LinearLayout>
然后在java文件里面写点击函数传入参数View表示实体也就是当前操作他的那个控件
创建私有对象变量Button a再在onCreate方法里面用id获取他->目的是:等会儿想在点击函数里写他怎么变
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
}
public void click(View v){
a.setText("按钮一被点击");
}
}
也可以按哪个Button哪个Button变不一定非要Button1,按照下面这么改
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
((Button)v).setText("按钮一被点击");
}
}
这种方法的关键是要导包import android.view.View;
下面两个案例
1.按按钮3变按钮1文字
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
android:onClick="click1"
/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
}
public void click1(View v){
a.setText("我爱你");
}
}
案例2:点击按钮3变最上面的文字(TextView里)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
android:onClick="click1"
/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
}
public void click1(View v){
a.setText("我爱你");
t.setText("qwe");
}
}
第二种方法匿名内部类
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
/>
</LinearLayout> 对按钮1实行匿名内部类来实现点击事件 package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); a=findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {这个函数名onClick不能变
a.setText("I love you!");
}
});
}
}
方法3 接口
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button a;
private Button b;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setText("I love you!");
}
});
b=findViewById(R.id.button2);
b.setOnClickListener(this);
}
public void click1(View v){
a.setText("我爱你");
t.setText("qwe");
}
@Override
public void onClick(View v) {
b.setText("WMY");
}
} <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
android:onClick="click2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
android:onClick="click1"
/>
</LinearLayout> 绿笔是关键,先写 b=findViewById(R.id.button2);
b.setOnClickListener(this); 然后this变红报错 然后点中this,Alt+Enter弹出这个选第二个实现接口
然后选第一个
最后在Onclick方法里写点击这个Button后你想干的
public void onClick(View v) {b.setText("WMY");
}
自适应(多语言)
第一步new resource file
第二步local,然后filename取strings
最后选地区ok
然后分别在每个语言的strings.xml下写代码,注意那个没有标语言的是默认strings.xml就是没有选语言默认显示
java文件
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.txt);
}
public void click001(View v){
txt.setText(R.string.app_name); //显示strings.xml里greetings里对应那句话
Toast.makeText(MainActivity.this,R.string.greetings,Toast.LENGTH_LONG).show();;
}
}
语言在设置里选
选完再打开app就是对应该语言下应该显示的(在他自己的strings.xml里设置)
标签:控件,layout,Button,第三,import,android,id,单元,View From: https://www.cnblogs.com/luckyhappyyaoyao/p/18078426