我正在为我的大学开发一个应用程序。但我被困在拨号选项。每次我点击呼叫按钮,即使选择了微调器的不同内容,拨号器上也会显示相同的号码。我想在拨号器上打开不同的号码,以选择不同的名称。
Java代码:
public class ContactTeacher extends Activity
{
List<String> spinnerArray1 = new ArrayList<String>();
List<String> spinnerArray2 = new ArrayList<String>();
List<String> spinnerArray3 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactstudent);
spinnerArray1.add("Select To Call");
spinnerArray1.add("Abhijit Dey");
spinnerArray1.add("Amit Ghosh Roy");
spinnerArray1.add("Tanay Bhadra");
spinnerArray1.add("Dhirodatta Subba");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item, spinnerArray1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = (Spinner) findViewById(R.id.studentspinner);
sItems.setAdapter(adapter);
spinnerArray2.add("Select Email");
spinnerArray2.add("Abhijit Dey");
spinnerArray2.add("Amit Ghosh Roy");
spinnerArray2.add("Tanay Bhadra");
spinnerArray2.add("Dhirodatta Subba");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item, spinnerArray2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems2 = (Spinner) findViewById(R.id.studentspinner2);
sItems2.setAdapter(adapter2);
spinnerArray3.add("Select Message");
spinnerArray3.add("Abhijit Dey");
spinnerArray3.add("Amit Ghosh Roy");
spinnerArray3.add("Tanay Bhadra");
spinnerArray3.add("Dhirodatta Subba");
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item, spinnerArray3);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems3 = (Spinner) findViewById(R.id.studentspinner3);
sItems3.setAdapter(adapter3);
}
public void stdcall(View v)
{
if(spinnerArray1.contains("Abhijit Dey"))
{
Intent b = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:000000000"));
startActivity(b);
}
else if(spinnerArray1.contains("Amit Ghosh Roy"))
{
Intent a = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:2222222222"));
startActivity(a);
}
else if(spinnerArray1.contains("Tanay Bhadra"))
{
Intent c = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:3333333333"));
startActivity(c);
}
else if(spinnerArray1.contains("Dhirodatta Subba"))
{
Intent d = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:44444444444"));
startActivity(d);
}
else
{
Intent e = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:55555555555"));
startActivity(e);
}
}
}
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Spinner
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/studentspinner"
android:layout_marginTop="67dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:spinnerMode="dialog" />
<Spinner
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/studentspinner2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:spinnerMode="dialog" />
<Spinner
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/studentspinner3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="69dp"
android:spinnerMode="dialog" />
<ImageButton
android:layout_width="70dp"
android:layout_height="50dp"
android:id="@+id/stdcallbutton"
android:layout_alignTop="@+id/studentspinner"
android:layout_toRightOf="@+id/studentspinner"
android:layout_toEndOf="@+id/studentspinner"
android:src="@drawable/call"
android:background="@null"
android:onClick="stdcall" />
<ImageButton
android:layout_width="70dp"
android:layout_height="50dp"
android:id="@+id/stdmailbutton"
android:layout_alignTop="@+id/studentspinner2"
android:layout_toRightOf="@+id/studentspinner2"
android:layout_toEndOf="@+id/studentspinner2"
android:src="@drawable/mail"
android:background="@null"
android:onClick="stdmail" />
<ImageButton
android:layout_width="70dp"
android:layout_height="50dp"
android:id="@+id/stdmsgbutton"
android:layout_alignBottom="@+id/studentspinner3"
android:layout_alignRight="@+id/stdmailbutton"
android:layout_alignEnd="@+id/stdmailbutton"
android:src="@drawable/message"
android:background="@null"
android:onClick="stdmsg" />
</RelativeLayout
答案 0 :(得分:0)
您的stdcall()
方法错误。你错误地使用了Spinner。
// Condition below is always true
// This way, the method always enter here regardless spinner seleciton
if(spinnerArray1.contains("Abhijit Dey"))
您应该检查当前微调器选定的项目。 如下所示:
public void stdcall(View v) {
Spinner sItems = (Spinner) findViewById(R.id.studentspinner);
if(sItems != null) {
Intent dialIntent = null;
switch(sItems.getSelectedItemPosition()) {
//case0 is spinner title... Ignore it
case 1:
//Abhijit Dey
dialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:000000000"));
break;
case 2:
//Amit Ghosh Roy
dialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:2222222222"));
break;
case 3:
//Tanay Bhadra
dialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:3333333333"));
break;
case 4:
//Dhirodatta Subba
dialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:44444444444"));
break;
}
if(dialIntent != null)
startActivity(dialIntent);
}
}