Android QPython3 扩展:图片命令菜单
#需要QPythonSupport基础窗口 https://www.bilibili.com/read/cv9100444
【QPythonSupport/app/src/main/AndroidManifest.xml】添加
<activity
android:label="图片命令列表"
android:name=".ImageCmd"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
【QPythonSupport/app/src/main/res/layout/imageCmd.xml】
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#af7f3f"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1" >
<TextView
android:id="@+id/ImageCmdTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:background="#3faf7f"
android:textColor="#ffffff"
android:textStyle="bold"
android:gravity="center"
/>
</ScrollView>
<ListView
android:id="@+id/ImageCmdList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/ImageCmdCancel"
android:textSize="25dp"
android:background="#7f7fff"
android:textColor="#ffffff"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
【QPythonSupport/app/src/main/res/layout/imageCmdView.xml】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageCmdViewImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/ImageCmdViewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:minHeight="50dp"
android:gravity="center_vertical"
android:textColor="#ffffff"
/>
</LinearLayout>
【QPythonSupport/app/src/main/java/czc/qpython/support/ImageCmd.java】
package czc.qpython.support;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import java.io.*;
import android.view.View.OnClickListener;
import android.net.*;
public class ImageCmd extends Activity
{
String[] items;
String[] images;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
Main.requestPermissions(this);
super.onCreate(savedInstanceState);
// Set main.xml as user interface layout
setContentView(R.layout.imageCmd);
Intent intent=getIntent();
String title=intent.getStringExtra("title");
items=intent.getStringArrayExtra("items");
try{
images=intent.getStringArrayExtra("images");
} catch(Exception E) {}
String cancel=intent.getStringExtra("cancel");
TextView Title=(TextView) findViewById(R.id.ImageCmdTitle);
Title.setText(title);
ListAdapter adapter = new ImageCmdAdapter(this, items);
ListView listView = (ListView) findViewById(R.id.ImageCmdList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id)
{
Intent intentR=new Intent();
intentR.putExtra("result",position);
ImageCmd.this.setResult(RESULT_OK,intentR);
ImageCmd.this.finish();
}
});
Button Cancel=(Button) findViewById(R.id.ImageCmdCancel);
Cancel.setText(cancel);
Cancel.setOnClickListener(new OnClickListener() {
public void onClick(View p)
{
ImageCmd.this.finish();
}
});
}
class ImageCmdAdapter extends ArrayAdapter<String>
{
public ImageCmdAdapter(Context context, String[] values)
{
super(context, R.layout.imageCmdView, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.imageCmdView, parent, false);
TextView Text = (TextView) view.findViewById(R.id.ImageCmdViewText);
Text.setText(items[position]);
ImageView Image = (ImageView) view.findViewById(R.id.ImageCmdViewImage);
try{
String img=images[position];
Image.setImageURI(Uri.fromFile(new File(img)));
} catch(Exception E){}
return view;
}
}
}
【QPythonSupport/__init__.py】添加
def ImageCmd(title='标题',items=('项目0',),images=(),cancel='取消'):
r=rsla('startActivityForResult','android.intent.action.VIEW',None,None,{'title':title,'items':items,'images':images,'cancel':cancel},'czc.qpython.support','czc.qpython.support.ImageCmd')
if r:
return r['extras']['result']
else:
return r
【QPythonSupport/说明.txt】添加
ImageCmd(title='标题',items=('项目0',),images=(),cancel='取消')
图片命令菜单(标题文本,各项目文本(项目1,项目2,……),各项目图片路径(图片1,图片2,……),取消按钮文本):
各项目文本items只能是tuple或list类型的str数组,且至少有一个项目;
各项目图片路径images只能是tuple或list类型的有效路径str数组;
部分项目无图片时,images可以部分项目为空str("");
全部项目无图片时,images可以为空白tuple或空白list;
当images项目数量<items项目数量时,items剩余项目没有图片;
当images项目数量>items项目数量时,多余图片会被忽略。

举例:

import os
A='/sdcard/Pictures/taobao/'
B=os.listdir(A)
C=list(B)
for d in range(len(C)):C[d]=A+C[d]
ImageCmd('淘宝图片',B,C)

import os
A='/sdcard/Pictures/taobao/'
B=os.listdir(A)
ImageCmd('淘宝图片',B)
点击第N个图片/文字项,返回(N-1),
点击“取消”,返回None。
视频:https://www.bilibili.com/video/BV1Ty4y1U7cp
作者:乘着船@Bilibili
更多文章+下载链接:https://www.bilibili.com/read/readlist/rl321663