我想构建一个Java类文件,该文件创建alertDialog并为sendSMS返回一个布尔值。但是我不知道如何从alertDialog java类返回值。所以我的函数返回空结果。
这是我第一个执行alertDialog java类的类
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SMSActivity extends AppCompatActivity {
ListView mainListView;
String mainCommandStrings[] = {"Turn ON", "Turn OFF", "Parameters"};
private static final String LOG_TAG = SMSActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
// Array Adapter
mainListView = findViewById(R.id.main_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
R.layout.sms_main_list_view,
R.id.command_text_view,
mainCommandStrings );
mainListView.setAdapter(arrayAdapter);
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View view, int position, long id){
switch (position){
case 0: // turn on
Toast.makeText(getApplicationContext(),"Turn on",Toast.LENGTH_SHORT).show();
ConfirmSMS confirmSMS =
new ConfirmSMS(getApplicationContext(), position);
sendTextMessage(confirmSMS, position);
break;
case 1: // Turn off
Toast.makeText(getApplicationContext(),"Turn off",Toast.LENGTH_SHORT).show();
ConfirmSMS confirmSMS1=
new ConfirmSMS(getApplicationContext(), position);
sendTextMessage(confirmSMS1, position);
break;
case 2: //Setting Parameters
Toast.makeText(getApplicationContext(),"Parameters",Toast.LENGTH_SHORT).show();
intentParameter();
break;
default:
break;
}
}
});
}
private void sendTextMessage(ConfirmSMS confirmSMS, int itemPosition) {
int position = itemPosition;
String stat;
if(position==0){
stat = "SMS Sent";
} else{
stat = "Cancel";
}
Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
}
private void intentParameter() {
Log.d(LOG_TAG, "SMS Method");
Intent parameterIntent = new Intent(this, ParameterActivity.class);
startActivity(parameterIntent);
}
这是我的alertDialog Java类(ConfirmSMS)
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;
class ConfirmSMS {
ConfirmSMS(final Context applicationContext, int itemPosition){
String stat = null;
if(itemPosition==0){
stat = "Turn ON";
} else if(itemPosition==1) {
stat = "Turn OFF";
}
final String condition = stat;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(applicationContext);
//Setting alert dialog
alertDialogBuilder.setTitle("Confirmation"); // set judul
alertDialogBuilder.setMessage("Are you sure to "+ condition+ " the AC?");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(applicationContext,
"Send instruction to "+condition+" the AC",
Toast.LENGTH_SHORT).show();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(applicationContext,
"Cancel",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
答案 0 :(得分:0)
重构代码,以便不需要其他类来创建AlertDialog。重新组织代码以将其全部保留在Activity中,以便您可以从AlertBuilder上的操作访问方法sendTextMessage。 像这样:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SMSActivity extends AppCompatActivity {
ListView mainListView;
String mainCommandStrings[] = {"Turn ON", "Turn OFF", "Parameters"};
private static final String LOG_TAG = SMSActivity.class.getSimpleName();
private boolean myBoolean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
// Array Adapter
mainListView = findViewById(R.id.main_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
R.layout.sms_main_list_view,
R.id.command_text_view,
mainCommandStrings);
mainListView.setAdapter(arrayAdapter);
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
String stat = null;
switch (position) {
case 0: // turn on
Toast.makeText(getApplicationContext(), "Turn on", Toast.LENGTH_SHORT).show();
stat = "Turn ON";
break;
case 1: // Turn off
Toast.makeText(getApplicationContext(), "Turn off", Toast.LENGTH_SHORT).show();
stat = "Turn ON";
break;
case 2: //Setting Parameters
Toast.makeText(getApplicationContext(), "Parameters", Toast.LENGTH_SHORT).show();
intentParameter();
break;
default:
break;
}
final String condition = stat;
if(condition != null){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
//Setting alert dialog
alertDialogBuilder.setTitle("Confirmation"); // set judul
alertDialogBuilder.setMessage("Are you sure to "+ condition+ " the AC?");
final AlertDialog.Builder builder = alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Send instruction to "+condition+" the AC",
Toast.LENGTH_SHORT).show();
//Here is where you execute sendTextMessage
//passing the param from the AlertDialog
sendTextMessage(position);
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Cancel",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
}
private void sendTextMessage(int itemPosition) {
int position = itemPosition;
String stat;
if (position == 0) {
stat = "SMS Sent";
} else {
stat = "Cancel";
}
Toast.makeText(getApplicationContext(), stat, Toast.LENGTH_SHORT).show();
}
private void intentParameter() {
Log.d(LOG_TAG, "SMS Method");
Intent parameterIntent = new Intent(this, ParameterActivity.class);
startActivity(parameterIntent);
}