android-styled-dialogs是一个github的自定义android对话框库,按照我上一篇blog的写法,可以将此项目导入ADT,进行开发。
上一篇blog地址为:http://blog.csdn.net/logicteamleader/article/details/45202863
今天介绍如何使用该库来创建自己的对话框,注意点有以下几点:
1.自己的项目必须依赖android-styled-dialogs-lib项目,仅仅导入lib项目生成的jar包是会运行报错的。
2.如果要修改theme,则须拷贝demo项目中的color.xml和theme.xml到本项目中的res/values目录下
其他就可以参考demo项目了。创建对话框非常简单,若要响应对话框则须继承ISimpleDialogListener接口和ISimpleDialogCancelListener接口并实现响应的方法,注意给每个对话框一个requestCode,这样在方法中就可以区分点击是来自哪个对话框。
参考代码如下,注意还须自己修改string.xml:
package com.example.wxb_example;
import eu.inmite.android.lib.dialogs.ISimpleDialogCancelListener;
import eu.inmite.android.lib.dialogs.ISimpleDialogListener;
import eu.inmite.android.lib.dialogs.ProgressDialogFragment;
import eu.inmite.android.lib.dialogs.SimpleDialogFragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements ISimpleDialogListener,ISimpleDialogCancelListener {
private MainActivity c = this ;
private static final int REQUEST_PROGRESS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTheme(R.style.DefaultLightTheme);
Button btn_message = (Button) findViewById(R.id.btn_message);
btn_message.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SimpleDialogFragment
.createBuilder(c, getSupportFragmentManager())
.setMessage(R.string.message1)
.setRequestCode(40)
.show();
}
});
Button btn_message_title = (Button) findViewById(R.id.btn_message_title);
btn_message_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SimpleDialogFragment
.createBuilder(c, getSupportFragmentManager())
.setTitle(R.string.title)
.setMessage(R.string.message2)
.setRequestCode(41)
.show();
}
});
findViewById(R.id.message_title_buttons_dialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleDialogFragment.createBuilder(c, getSupportFragmentManager())
.setTitle(R.string.title)
.setMessage(R.string.message_3)
.setPositiveButtonText(R.string.positive_button)
.setNegativeButtonText(R.string.negative_button).setRequestCode(42)
.setTag("custom-tag")
.show();
}
});
findViewById(R.id.progress_dialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialogFragment.createBuilder(c, getSupportFragmentManager())
.setMessage(R.string.message_4)
.setRequestCode(REQUEST_PROGRESS)
.setTitle(R.string.app_name)
.show();
}
});
}
@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);
}
@Override
public void onPositiveButtonClicked(int requestCode) {
// TODO Auto-generated method stub
switch(requestCode){
case 42:
Toast.makeText(c,"onPositiveButtonClicked", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onNegativeButtonClicked(int requestCode) {
// TODO Auto-generated method stub
switch(requestCode){
case 42:
Toast.makeText(c,"onNegativeButtonClicked", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onCancelled(int requestCode) {
// TODO Auto-generated method stub
Toast.makeText(c,"onCancelled"+requestCode, Toast.LENGTH_LONG).show();
}
}