Activity是Android四大组件之一,为用户提供与系统交互的界面,对页面进行逻辑的操作
例如:QQ的界面每个人信息不一样,点击登陆按钮后的操作等等
首先,在对控件进行交互时,要获得控件。通过**findViewById(R.id.控件id)**获得控件。然后可以对控件进行操作,下面是简单的演示。(关于R,创建的每一个工程出来src文件夹之外还有一个gen文件夹,gen里面有一个R.java,你在res目录下使用的图片,布局等资源都会在gen目录里的R。java自动生成对应的id。方便你调用)
点击事件
1.实现类继承View.OnClickListener并且重写onClick方法
2.匿名类实现
3.拉姆达表达式实现。用此方法接口名字可以省略,方法名字也可以省略,但接口中只能有一个方法。
详细见代码
Log.i(“标签名字”,“打印内容”);
Activity代码:
package com.hnucm.android_03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过代码修改文本的值
// 第一步 拿到文本的引用 根据文本的内存地址找到这个控件
TextView textView= findViewById(R.id.textView5);
textView.setText("湖南中医药大学");
//给浮点类型的数据,浮点类型可以转成sp
textView.setTextSize(50);
//拿到按钮的引用
Button button = findViewById(R.id.button);
//创建了一个类,实现了接口,匿名内部类
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("MainActivity","MyOnClick");
}
});
//接口的名字也可以省略,方法的名字也可以省略,参数名字也可以省略
//拉姆达表达式 接口只能有一个方法
button.setOnClickListener(
(v) -> {
Log.i("MainActivity","MyOnClick");
}
);
}
}
这里用到了在app中打印的方法。
核心代码:Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
这个方法需要传递三个参数,第一个是Context,是Toast的上下文,由于活动本身继承了Context对象,使用当前Activity即可。第二个参数是Toast显示的内容。第三个参数是Toast显示的时长。
Activity代码
package com.hnucm.android_03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText usernameText = findViewById(R.id.editTextTextPersonName);
EditText passwordText = findViewById(R.id.editTextTextPersonName2);
// MainActivity的父类的父类的父类的......是Context
Button button=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameText.getText().toString();
String password = passwordText.getText().toString();
if(username.equals("hnucm")&&password.equals("123456")){
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_LONG).show();
}
}
});
}
}
在获得文本控件的数值之后,经过计算返回结果给另一个文本控件
Activity代码
package com.hnucm.android_03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText num1 = findViewById(R.id.textView2);
EditText num2 = findViewById(R.id.textView3);
EditText result=findViewById(R.id.textView4);
// MainActivity的父类的父类的父类的......是Context
Button button=findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = Integer.parseInt(num1.getText().toString());
int j = Integer.parseInt(num2.getText().toString());
result.setText((i+j)+"");
}
});
}
}
预告:过几天会将实现的多个数字加减乘除的计算器代码放出来(虽然已经完成了,因为这个是作业,很多同学还未完成,作业截止后发出)先放几张图。
xml布局
<androidx.constraintlayout.widget.ConstraintLayout 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/five"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="5"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/two"
app:layout_constraintEnd_toStartOf="@+id/six"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/four" />
<TextView
android:id="@+id/six"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="6"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/three"
app:layout_constraintEnd_toStartOf="@+id/minus"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/five" />
<TextView
android:id="@+id/seven"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="7"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/four"
app:layout_constraintEnd_toStartOf="@+id/eight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/nine"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="9"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/six"
app:layout_constraintEnd_toStartOf="@+id/multiplication"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/eight" />
<TextView
android:id="@+id/multiplication"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="*"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/minus"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/nine" />
<TextView
android:id="@+id/eight"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="8"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/five"
app:layout_constraintEnd_toStartOf="@+id/nine"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/seven" />
<TextView
android:id="@+id/zero"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="0"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/point"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/op" />
<TextView
android:id="@+id/add"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="+"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/equal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/three" />
<TextView
android:id="@+id/one"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="1"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/op"
app:layout_constraintEnd_toStartOf="@+id/two"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/three"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="3"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/point"
app:layout_constraintEnd_toStartOf="@+id/add"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/two" />
<TextView
android:id="@+id/minus"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="-"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/six" />
<TextView
android:id="@+id/two"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="2"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/zero"
app:layout_constraintEnd_toStartOf="@+id/three"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/one" />
<TextView
android:id="@+id/four"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="4"
android:background="?android:attr/selectableItemBackground"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/one"
app:layout_constraintEnd_toStartOf="@+id/five"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/point"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="."
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/equal"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/zero" />
<ImageView
android:id="@+id/op"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:background="?android:attr/selectableItemBackground"
android:src="@drawable/op"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/zero"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/clear1"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:src="@drawable/cancel"
android:text="TextView"
android:background="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toTopOf="@+id/eight"
app:layout_constraintEnd_toStartOf="@+id/present"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/ac" />
<TextView
android:id="@+id/ac"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="AC"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/seven"
app:layout_constraintEnd_toStartOf="@+id/clear1"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/division"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="/"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/multiplication"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/present" />
<TextView
android:id="@+id/present"
android:layout_width="104dp"
android:layout_height="70dp"
android:gravity="center"
android:text="%"
android:background="?android:attr/selectableItemBackground"
android:textColor="#FF7433"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/nine"
app:layout_constraintEnd_toStartOf="@+id/division"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/clear1" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:ems="10"
android:gravity="right"
android:focusable="false"
android:inputType="textPersonName"
android:textSize="40sp"
app:layout_constraintBottom_toTopOf="@+id/clear1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/equal"
android:layout_width="104dp"
android:layout_height="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/point"
android:background="?android:attr/selectableItemBackground"
app:srcCompat="@drawable/equal" />
androidx.constraintlayout.widget.ConstraintLayout>
MainActivity代码
package com.hnucm.c201901020241;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.sql.SQLOutput;
import java.util.Vector;
public class MainActivity extends AppCompatActivity {
int ju=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String pre=null;
TextView clear = findViewById(R.id.ac);//清空
ImageView equal = findViewById(R.id.equal);//等于
ImageView clear1=findViewById(R.id.clear1);//删除一个
final EditText result = findViewById(R.id.editText);//显示运算
TextView add = findViewById(R.id.add);//加
TextView minus = findViewById(R.id.minus);//减
TextView multiplication = findViewById(R.id.multiplication);//乘
TextView division = findViewById(R.id.division);//除
TextView remainder = findViewById(R.id.present);//取余
TextView zero = findViewById(R.id.zero);//0
TextView one = findViewById(R.id.one);//1
TextView two = findViewById(R.id.two);//2
TextView three = findViewById(R.id.three);//3
TextView four = findViewById(R.id.four);//4
TextView five = findViewById(R.id.five);//5
TextView six = findViewById(R.id.six);//6
TextView seven = findViewById(R.id.seven);//7
TextView eight = findViewById(R.id.eight);//8
TextView nine = findViewById(R.id.nine);//9
TextView point=findViewById(R.id.point);//.
class onClick implements View.OnClickListener {
@Override
public void onClick(View v) {
System.out.println(v);
String str = result.getText().toString();
if(ju==1){
ju=0;
str="";
}
if (v instanceof TextView) {
if(v.getId()==R.id.ac)result.setText("");
else{
char k='1';
String now=((TextView) v).getText().toString();
if(str.length()>1) k=str.charAt(str.length()-1);
if((now.equals("+")||now.equals("-")||now.equals("*")||now.equals("/"))&&(k=='+'||k=='-'||k=='*'||k=='/')) {
result.setText(str.substring(0,str.length()-1) + ((TextView) v).getText().toString());
System.out.println(now);
System.out.println(k);
}
else result.setText(str+((TextView) v).getText().toString());
}
}
else if(v instanceof ImageView){
if(v.getId()==R.id.clear1){//删除一个
if(str!=null&&!str.equals("")){
result.setText(str.substring(0,str.length()-1));
}
}
else if(v.getId()==R.id.equal) {
str+="=";
result.setText(str);
String sum = getResult(result.getText().toString());
result.setText(str+sum);
ju=1;
}
}
}
}
zero.setOnClickListener(new onClick());
one.setOnClickListener(new onClick());
two.setOnClickListener(new onClick());
three.setOnClickListener(new onClick());
four.setOnClickListener(new onClick());
five.setOnClickListener(new onClick());
six.setOnClickListener(new onClick());
seven.setOnClickListener(new onClick());
eight.setOnClickListener(new onClick());
nine.setOnClickListener(new onClick());
add.setOnClickListener(new onClick());
minus.setOnClickListener(new onClick());
multiplication.setOnClickListener(new onClick());
division.setOnClickListener(new onClick());
remainder.setOnClickListener(new onClick());
clear.setOnClickListener(new onClick());
result.setOnClickListener(new onClick());
equal.setOnClickListener(new onClick());
clear1.setOnClickListener(new onClick());
point.setOnClickListener(new onClick());
}
public String getResult(String s){//开始计算
if(s==null||s.equals(""))return "";
double num=0;
int i,flag=0,ju=0;
double temp=0;
Vector<Double> a = new Vector<Double>();
Vector<Character>b=new Vector<Character>();
char op1,op2;
for(i=0;i<s.length();i++){//将数字和操作符存下来
char k=s.charAt(i);
if(k>='0'&&k<='9'&&flag==0){
temp*=10;
temp+=k-'0';ju=1;
}
else if(k>='0'&&k<='9'&&flag>0){
temp+=(k-'0')/Math.pow(10,flag);
flag++;
ju=1;
}
else if(k=='.')flag=1;
else {
a.add(temp);b.add(k);temp=0;flag=0;ju=0;
}
}
if(ju==1)a.add(temp);
if(a.size()==1)return a.get(0)+"";
int ans=1;
char pre,now;
pre=b.get(0);
double l=a.get(0),r=a.get(1);
for(i=2;i<a.size();i++){
double rr=a.get(i);
now=b.get(ans++);
if(now=='+'||now=='-'){
if(pre=='+') {
l += r;
r = rr;
}
else if(pre=='-'){
l-=r;
r=rr;
}
else if(pre=='*'){//这两种情况只在第一个预算符是乘除时出现
l*=r;
r=rr;
}
else if(pre=='/'){
if(r==0)return "ERROR";
l/=r;
r=rr;
}
else if(pre=='%'){
l%=r;
r=rr;
}
pre=now;
}
else if(now=='*'){
r=rr*r;
}
else if(now=='/'){
if(rr==0)return "ERROR";
r=r/rr;
}
else if(now=='%'){
r=r%rr;
}
}
if(pre=='+') return l+r+"";
else if(pre=='-')return l-r+"";
else if(pre=='*')return l*r+"";
else if(pre=='/'){
if(r!=0) return l/r+"";
else return "ERROR";
}
else if(pre=='%')return (int)l%(int)r+"";
else return "";
}
}
出现错误解决方法(在模拟器中出现错误)
报错了,没有这个id
1.查看日志
2.分析错误的位置
欢迎批评指正,未完待续…