我正在创建一个电话本应用,其中已经加载了电子邮件。![布局中有名称和数字。我已经定义了数字。] [1] 应用图片位于此链接中:http://imgur.com/TPIupGg
这是xml文件。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tushar.ruphonedirectory15.ACCE"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
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"
android:orientation="vertical"
android:background="#ffffb14d"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/press"
android:textColor="#ffff2c2e"
android:layout_marginBottom="20dp"
android:id="@+id/textView79" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/sovapoti"
android:id="@+id/textView73"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/acce1"
android:id="@+id/textView74"
android:layout_below="@+id/textView73"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac1"
android:id="@+id/button3"
android:layout_below="@+id/textView74"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:background="@drawable/make"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac2"
android:id="@+id/button64"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:background="@drawable/make"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/profesor"
android:id="@+id/textView75"
android:layout_below="@+id/button64"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/acce2"
android:id="@+id/textView76"
android:layout_below="@+id/textView75"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac3"
android:id="@+id/button65"
android:layout_below="@+id/textView76"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:background="@drawable/make"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/acce3"
android:id="@+id/textView77"
android:layout_below="@+id/button66"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac4"
android:id="@+id/button66"
android:layout_below="@+id/button65"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:background="@drawable/make"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac5"
android:id="@+id/button67"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView77" android:background="@drawable/make"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/acce4"
android:id="@+id/textView78"
android:layout_below="@+id/button68"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff4cd6ff"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ac6"
android:id="@+id/button68"
android:layout_below="@+id/button67"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:background="@drawable/make"
android:layout_marginBottom="10dp"/>
我不得不编写java代码来拨打这个号码。但问题是我必须为每个数字和每个按钮编写java代码。这使得java代码冗长.java代码如下所示。所以有什么办法对所有按钮使用相同的方法(拨号)。因此可以减少java代码。电话号码保存在值&gt;字符串中。
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ACCE extends ActionBarActivity {
Button a1;
Button a2;
Button a3;
Button a4;
Button a5;
Button a6;
Button a7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acce);
a1= (Button) findViewById(R.id.button3);
a2= (Button) findViewById(R.id.button64);
a3= (Button) findViewById(R.id.button65);
a4= (Button) findViewById(R.id.button66);
a5= (Button) findViewById(R.id.button67);
a6= (Button) findViewById(R.id.button68);
a7= (Button) findViewById(R.id.button69);
a1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m1();
}
});
a2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m2();
}
});
a3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m3();
}
});
a4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m4();
}
});
a5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m5 ();
}
});
a6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m6 ();
}
});
a7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m7 ();
}
});
protected void m1() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:0721711106"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m2() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:0721810009"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m3() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:01191536769"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m4() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:0721772307"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m5() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:01914254694"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m6() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:0721750498"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
protected void m7() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:01712255636"));
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making call", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ACCE.this,
"Call faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
为什么必须为每个按钮定义?就这样做吧
a1.setOnClickListener(this);
a2.setOnClickListener(this);
.....
a7.setOnClickListener(this);
现在让您的类实现OnClickListener并定义Overridden onClick()。
您将获得所有点击的事件。