20170123 AlertDialog練習_在交談窗中加入按鈕
這個是AlertDialog練習,在交談窗中加入按鈕!
在這之中最多加入三個按鈕,否(Negative)中性(Neutral)及是(Positive)
加入的方法為setXxxButton() 而其中的Xxx分別為Negativity Neutral positive
//AlertDialog練習在交談窗中加入按鈕
//20170124 劉彥廷
MainActivity.java
package com.example.user.myapplication41;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener {
TextView txv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView)findViewById(R.id.answer);
new AlertDialog.Builder(this)
.setMessage("你喜歡使用這支手機嗎?")
.setCancelable(false)
.setIcon(android.R.drawable.ic_menu_edit)
.setTitle("Android問卷調查")
.setPositiveButton("喜歡",this)
.setNegativeButton("討厭",this)
.setNeutralButton("沒意見",null)
.show();
}
@Override
public void onClick(DialogInterface dialog, int id) {
if(id == DialogInterface.BUTTON_POSITIVE){
txv.setText("你喜歡這支手機");
}
else if(id ==DialogInterface.BUTTON_NEGATIVE){
txv.setText("你不喜歡這支手機");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.user.myapplication41.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txv"
android:id="@+id/answer"
android:textSize="30sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="87dp" />
</RelativeLayout>
}
在這之中最多加入三個按鈕,否(Negative)中性(Neutral)及是(Positive)
加入的方法為setXxxButton() 而其中的Xxx分別為Negativity Neutral positive
//AlertDialog練習在交談窗中加入按鈕
//20170124 劉彥廷
MainActivity.java
package com.example.user.myapplication41;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener {
TextView txv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView)findViewById(R.id.answer);
new AlertDialog.Builder(this)
.setMessage("你喜歡使用這支手機嗎?")
.setCancelable(false)
.setIcon(android.R.drawable.ic_menu_edit)
.setTitle("Android問卷調查")
.setPositiveButton("喜歡",this)
.setNegativeButton("討厭",this)
.setNeutralButton("沒意見",null)
.show();
}
@Override
public void onClick(DialogInterface dialog, int id) {
if(id == DialogInterface.BUTTON_POSITIVE){
txv.setText("你喜歡這支手機");
}
else if(id ==DialogInterface.BUTTON_NEGATIVE){
txv.setText("你不喜歡這支手機");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.user.myapplication41.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txv"
android:id="@+id/answer"
android:textSize="30sp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="87dp" />
</RelativeLayout>
}
strings.xml
<resources>
<string name="app_name">問卷調查(No41)</string>
<string name="txv">你對Android手機沒意見</string>
</resources>
留言
張貼留言