上篇文章介绍了使用注册上下文 菜单的方式实现了类似自定义对话框的效果,这次换个方法,使用Activity的方式实现,先看效果
长按测试1
然后再点击选项1
下面是实现方法:
1.在AndroidManifest.xml中把Activity的Theme改为MyDialogStyle
<activity
android:name="com.example.test.TestActivityForDialog"
android:screenOrientation="portrait"
android:theme="@style/MyDialogStyle" />
MyDialogStyle在style.xml文件中
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
代码如下:
MainActivity
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final int RESULT_CODE_1 = 1;
public static final int RESULT_CODE_2 = 2;
public static final int TESTFORDIALOG = 0;
private ListView mListView;
private String msg[] = new String[] { "测试1", "测试2", "测试3", "测试4" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, msg);
mListView.setAdapter(adapter);
mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,
TestActivityForDialog.class);
startActivityForResult(intent, TESTFORDIALOG);
return false;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CODE_1) {
Toast.makeText(this, "选项1被选择了", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CODE_2) {
Toast.makeText(this, "选项2被选择了", Toast.LENGTH_SHORT).show();
}
}
}
TestActivityForDialog
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
/**
* @ClassName: TestActivityForDialog
* @author: wxj
* @date: 2015年11月26日 下午2:15:04
* @Description:
*/
public class TestActivityForDialog extends Activity implements OnClickListener {
private static final int RESULT_CODE_1 = 1;
private static final int RESULT_CODE_2 = 2;
private TextView tv_item1;
private TextView tv_item2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_activity_for_dialog);
setUpView();
}
private void setUpView() {
tv_item1 = (TextView) findViewById(R.id.tv_item1);
tv_item2 = (TextView) findViewById(R.id.tv_item2);
tv_item1.setOnClickListener(this);
tv_item2.setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return super.onTouchEvent(event);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_item1:
setResult(RESULT_CODE_1);
finish();
break;
case R.id.tv_item2:
setResult(RESULT_CODE_2);
finish();
break;
default:
break;
}
}
}
布局文件:
activity_main
<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:background="@android:color/white"
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.test.MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_test_activity_for_dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="15dp"
android:text="选项1"
android:textColor="@android:color/primary_text_light" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/tv_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="15dp"
android:text="选项2"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
OK,大功告成