码迷,mamicode.com
首页 > 移动开发 > 详细

Android OnClick的四种方法

时间:2020-03-17 10:16:07      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:public   case   klist   err   override   creat   create   make   电话号码   

Android OnClick的四种方法

XML文件

<Button
        android:id="@+id/bt_dial"
/>
<EditText
    android:id="@+id/et_number"
 />

①匿名内部类

java文件

Button bt_dial = (Button) findViewById(R.id.bt_dial);
bt_dial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialPhone();
}
});


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bt_dial = (Button) findViewById(R.id.bt_dial);
    bt_dial.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialPhone();
        }
    });
}

②自定义单击事件监听类
{
Button bt_dial = (Button) findViewById(R.id.bt_dial);
bt_dial.setOnClickListener(new MyListener());
}
private class MyListener implements View.OnClickListener {
@Override
public void onClick(View view) {
dialPhone();
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText) MainActivity.this.findViewById(R.id.et_number);                    
        Button bt_dial = (Button) findViewById(R.id.bt_dial);
        bt_dial.setOnClickListener(new MyListener());
    } 
    private class MyListener implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        dialPhone();
    }
}

③Activity继承View.OnClickListener

public class MainActivity extends Activity implements View.OnClickListener {
    EditText et_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_number = (EditText)    MainActivity.this.findViewById(R.id.et_number);        
    Button bt_dial = (Button) findViewById(R.id.bt_dial);
        bt_dial.setOnClickListener(this); 
    }
 
    public void onClick(View view) { 
        switch (view.getId()){
            case R.id.bt_dial:
                dialPhone();
                break;
            default:
                break;
        }
    }
}

④在XML文件中显示指定按钮的onClick属性

<Button
        android:onClick="dialPhoneMethod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dial_number"
        android:id="@+id/bt_dial"
        android:layout_below="@id/et_number"
        android:layout_alignRight="@id/et_number"
        android:layout_alignEnd="@id/et_number" />

在 android:onClick="dialPhoneMethod"处点击Alt+Enter
选择“create "OnClick(View)”in XXXX",表示在XXXX.java文件中新建点击事件

public void dialPhoneMethod(View view){
        dialPhone();
    }
 
    private void dialPhone() {
        String number = et_number.getText().toString().trim();
 
        if (TextUtils.isEmpty(number)){
            Toast.makeText(this, "电话号码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + number));
        startActivity(intent);
    }

Android OnClick的四种方法

标签:public   case   klist   err   override   creat   create   make   电话号码   

原文地址:https://www.cnblogs.com/xyann/p/12508897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!