我正在创建微调器对话框以关闭并重新启动我在keydown上显示的内容。问题是我不想在每个活动上创建微调器。那么如何在应用程序中的所有活动中调用单个微调器?
public class MainActivity extends Activity {
Spinner spinner;
String[] strings = {"Power Options","Power off","Restart"};
String[] subs = {"","Shut phone down","Close all apps and restart phone" };
int arr_images[] = {R.drawable.ic_launcher, R.drawable.images1,
R.drawable.images2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new MyAdapter(this, R.layout.row, strings));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
// TODO Auto-generated method stub
if (i == 1)
{
Toast.makeText(MainActivity.this,"PowerOff
Perform",Toast.LENGTH_SHORT).show();
}
if (i == 2)
{
Toast.makeText(MainActivity.this,"Reboot Perform",Toast.LENGTH_SHORT).show();
}
}
// If no option selected
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId, String[] objects)
{
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(subs[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_PAGE_UP == event.getKeyCode() && isRooted()){
spinner.performClick();
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:0)
创建一个名为BaseActivity的类并将其扩展为Activity。然后,您可以从BaseActivity类创建活动并向其添加对话框。
编辑:
public class BaseActivity extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
}
,您的MainActivity将扩展BaseActivity。