80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.cellphone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_dial=(Button) findViewById(R.id.button1);
bt_dial.setOnClickListener(new MyListener());
Button bt2=(Button) findViewById(R.id.button2);
bt2.setOnClickListener(new MyListener());
}
private class MyListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText et_number=(EditText) MainActivity.this.findViewById(R.id.et_number);
String number=et_number.getText().toString();
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="InputTelNum"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Button"
/>
</LinearLayout>
private void initUi() {
mTelNum = (EditText) findViewById(R.id.editText);
btn1 = (Button) findViewById(R.id.btn1);
mTelNum.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
btn1.setText(mTelNum.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num=btn1.getText().toString();
Intent i=new Intent();
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:"+num));
startActivity(i);
}
});
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/et_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="phone" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginRight="100dp"
android:layout_marginTop="39dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="14dp"
android:text="老婆" />
</RelativeLayout>