80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.ch02_dan;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
final String[] arrayFruit=new String[]{"苹果","橘子","草莓","香蕉"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.bt1:
AlertDialog dia=new AlertDialog.Builder(this ).create();
dia.setTitle("你喜欢吃哪种水果?");
dia.setSingleChoiceItems(arrayFruit,0,listen);
dia.setButton(AlertDialog.BUTTON_POSITIVE,"确定",listener);
dia.setButton(AlertDialog.BUTTON_NEGATIVE,"取消",liss);
dia.show();
}
}
DialogInterface.OnClickListener listen=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
selectedFruitIndex=which;
}
};
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
Toast toast1=Toast.makeText(DialogDemoActivity.this,arrayFruit[selectedFruitIndex],Toast.LENGTH_SHORT);
toast1.show();
}
};
DialogInterface.OnClickListener liss=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_dan.MainActivity" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xuan"
android:textSize="20sp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">单选框</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="xuan">选择</string>
</resources>
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends Activity implements OnClickListener {
final String[] arrayFruit = new String[]{"苹果", "橘子", "草莓", "香蕉"};
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
private int selectedFruitIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.bt1);
button.setOnClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
AlertDialog.Builder dia = new AlertDialog.Builder(this);
dia.setTitle("你喜欢吃哪种水果?");
dia.setSingleChoiceItems(arrayFruit, selectedFruitIndex, listen);
dia.setPositiveButton("确定", listener);
dia.setNegativeButton("取消", liss);
dia.show();
}
}
DialogInterface.OnClickListener listen = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
};
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
};
DialogInterface.OnClickListener liss = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = 0;
}
};
}
已经可以运行了,给分吧,关于 dialog 建议你还是多去百度下,这些基本的东西你都没弄懂额.package com.example.ch02_dan;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
final String[] arrayFruit=new String[]{"苹果","橘子","草莓","香蕉"};
private int selectedFruitIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
Button butt=(Button)findViewById(R.id.bt2);
butt.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.bt1:
AlertDialog dia=new AlertDialog.Builder(this ).create();
dia.setTitle("你喜欢吃哪种水果?");
dia.setSingleChoiceItems(arrayFruit,0,listen);
dia.setButton(AlertDialog.BUTTON_POSITIVE,"确定",listener);
dia.setButton(AlertDialog.BUTTON_NEGATIVE,"取消",liss);
dia.show();
break;
case R.id.bt2:
AlertDialog di=new AlertDialog.Builder(this ).create();
di.setTitle("请输入:");
di.setView(new EditText(this));
di.setButton(AlertDialog.BUTTON_POSITIVE,"确定",listener);
di.setButton(AlertDialog.BUTTON_NEGATIVE,"取消",liss);
di.show();
break;
}
}
DialogInterface.OnClickListener listen=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
selectedFruitIndex=which;
}
};
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
Toast toast1=Toast.makeText(MainActivity.this,arrayFruit[selectedFruitIndex],Toast.LENGTH_SHORT);
toast1.show();
}
};
DialogInterface.OnClickListener liss=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
dia.setSingleChoiceItems(arrayFruit,0,listen);这个一直报错