Android开发UI布局之计算机点击事件处理练习

一、目标

学习通过点击来触发事件,以计算机为例

二、源代码

第一种方法:通过在layout里面不同的view中加入onclick属性

 然后在调用这个layout的activity里写相应同名的方法,方法的格式是固定的

public void 方法名(View view){

…..

}

public class MainActivity extends AppCompatActivity {

    public static final String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.counterlayout_presstest);

    }

    
    public void onWhite(View view){
        Log.d(TAG, " 嘿嘿 我点了一个白块,它的值为:"+((TextView)view).getText().toString());
    }
    public void onOrange(View view){
        Log.d(TAG, " 嘿嘿 我点了一个橙块,它的值为:"+((TextView)view).getText().toString());
    }
}
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--计算器点击事件实现练习-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="C"
            android:textSize="30dp"
            android:background="@drawable/selector_white"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="+/-"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="%"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onOrange"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="÷"
            android:textSize="30dp"
            android:gravity="center"

            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="1"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="2"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="3"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onOrange"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="X"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="4"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:text="5"
            android:textSize="30dp" />

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="6"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onOrange"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="--"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="7"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="8"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:text="9"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onOrange"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="+"
            android:textSize="30dp"
            android:gravity="center"

            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="0"
            android:textSize="30sp"
            android:paddingLeft="42dp"
            android:gravity="center_vertical"
            android:background="@drawable/selector_white"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onWhite"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="."
            android:textSize="30sp"
            android:background="@drawable/selector_white"
            android:gravity="center"
            android:layout_height="match_parent"/>

        <TextView
            android:onClick="onOrange"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="@drawable/selector_orange"
            android:text="="
            android:textSize="30sp"
            android:gravity="center"
            android:layout_height="match_parent"/>
    </LinearLayout>

</LinearLayout>
Layout

第二种方法:首先是给控件设置id,然后通过findViewById来找到控件,有了控件以后给控件设置点击事件

public class MainActivity extends AppCompatActivity {

    public static final String TAG="MainActivity";
    private TextView cancel;
    private TextView tv_true_false;
    private TextView tv_mod;
    private TextView tv_divide;
    private TextView tv_one;
    private TextView tv_two;
    private TextView tv_three;
    private TextView tv_multiply;
    private TextView tv_four;
    private TextView tv_five;
    private TextView tv_six;
    private TextView tv_subtract;
    private TextView tv_seven;
    private TextView tv_eight;
    private TextView tv_nine;
    private TextView tv_plus;
    private TextView tv_zero;
    private TextView tv_point;
    private TextView tv_equal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.counterlayout_presstest);
        //找控件
        initView();
        //设置点击事件
        initClickEvent();

    }

    /**
     *设置点击事件
     */
    private void initClickEvent() {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_true_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_mod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_divide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
        tv_equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "这个按钮得值为: "+((TextView)v).getText().toString());
            }
        });
    }

    /**
     *在这里找控件
     */
    private void initView() {
        cancel = this.findViewById(R.id.tv_cancel);
        tv_true_false = this.findViewById(R.id.tv_true_false);
        tv_mod = this.findViewById(R.id.tv_mod);
        tv_divide = this.findViewById(R.id.tv_divide);
        tv_one = this.findViewById(R.id.tv_one);
        tv_two = this.findViewById(R.id.tv_two);
        tv_three = this.findViewById(R.id.tv_three);
        tv_multiply = this.findViewById(R.id.tv_multiply);
        tv_four = this.findViewById(R.id.tv_four);
        tv_five = this.findViewById(R.id.tv_five);
        tv_six = this.findViewById(R.id.tv_six);
        tv_subtract = this.findViewById(R.id.tv_subtract);
        tv_seven = this.findViewById(R.id.tv_seven);
        tv_eight = this.findViewById(R.id.tv_eight);
        tv_nine = this.findViewById(R.id.tv_nine);
        tv_plus = this.findViewById(R.id.tv_plus);
        tv_zero = this.findViewById(R.id.tv_zero);
        tv_point = this.findViewById(R.id.tv_point);
        tv_equal = this.findViewById(R.id.tv_equal);
    }
}
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--计算器布局练习-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:text="C"
            android:textSize="30dp"
            android:background="@drawable/selector_white"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_true_false"
            android:layout_width="0dp"
            android:text="+/-"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_mod"
            android:layout_width="0dp"
            android:text="%"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_divide"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="÷"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:id="@+id/tv_one"
            android:layout_width="0dp"
            android:text="1"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_two"
            android:layout_width="0dp"
            android:text="2"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_three"
            android:layout_width="0dp"
            android:text="3"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_multiply"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="X"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:id="@+id/tv_four"
            android:layout_width="0dp"
            android:text="4"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_five"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:text="5"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/tv_six"
            android:layout_width="0dp"
            android:text="6"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_subtract"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="--"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:id="@+id/tv_seven"
            android:layout_width="0dp"
            android:text="7"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_eight"
            android:layout_width="0dp"
            android:text="8"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_nine"
            android:layout_width="0dp"
            android:text="9"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/selector_white"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_plus"
            android:layout_width="0dp"
            android:background="@drawable/selector_orange"
            android:text="+"
            android:textSize="30dp"
            android:gravity="center"

            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_zero"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="0"
            android:textSize="30sp"
            android:paddingLeft="42dp"
            android:gravity="center_vertical"
            android:background="@drawable/selector_white"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_point"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="."
            android:textSize="30sp"
            android:background="@drawable/selector_white"
            android:gravity="center"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_equal"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="@drawable/selector_orange"
            android:text="="
            android:textSize="30sp"
            android:gravity="center"
            android:layout_height="match_parent"/>
    </LinearLayout>

</LinearLayout>
Layout

三、心得体会

两种点击事件触发的方式略有不同,主要区别在把事件和控件相对应的方式上,具体实现我认为应该按照控件的多少和发生的事件相同和不同的程度上,如果控件少发生事件相同或种类较少选择第一种合适,相反选择第二种较为合适

原文地址:https://www.cnblogs.com/suanai/p/12249198.html