gpt4 book ai didi

java - 如何创建当我单击 ListView (android studio)中的项目时显示的弹出菜单?

转载 作者:行者123 更新时间:2023-12-03 02:49:35 25 4
gpt4 key购买 nike

我是 Android 开发新手...我在setting.java中使用字符串和数组适配器创建了一个 ListView :

public class setting extends Activity {
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_layout);
String[] settingOptions = new String[]{getString(R.string.settingInterface), getString(R.string.settingLanguages), getString(R.string.settingInfo)};
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, settingOptions);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}

});



}
}

这是我的setting_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>

setting_layout Activity(listView)

我想为语言项目创建一个弹出菜单,当我单击它时,会显示一些其他语言的翻译。我已经用其他语言创建了 string.xml,但我不知道如何在弹出菜单中使用它。1:如何让点击语言项时显示弹出菜单?2:如何在弹出菜单中放入其他语言?提前致谢

最佳答案

对于弹出窗口,您可以使用对话框生成器创建一个对话框,它可以是自定义对话框(xml 布局),也可以动态创建(使用 java 以编程方式)查看此代码以获取弹出 View :)

例如,我有一个在 onClick 中调用的函数:

private  void showFlavorDialog(){

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
View promptView;
LayoutInflater layoutInflater = LayoutInflater.from(myActivity.this); //this gets the custom layout inflater
promptView = layoutInflater.inflate(R.layout.dialog_flavors_ar, null); // here you put the custom xml leyout name
final EditText editText = (EditText) promptView.findViewById(R.id.editText_note); //my layout has an edit text and button in it and I want to use it, so call the findviewbyid
Button add_note = (Button) promptView.findViewById(R.id.button_note);
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //this removes full screen keyboard, so the keyboard doesnt take the whole screen.
add_note.setOnClickListener(new View.OnClickListener() { //onClick for the button
@Override
public void onClick(View v) {
otherNote_b = true;
flavor = editText.getText().toString();
new httpSetFlavor().execute();
alertDialogFL.cancel();
}
});
if (selectedflavorNameEn.size() > 1) {
flavorsGrid = (GridView) promptView.findViewById(R.id.gridView_flavors); // I also have a gridview in the layout
flavorsGridAdapter adapter = new flavorsGridAdapter(OrderActivityAr.this,
selectedflavorNameEn);
flavorsGrid.setAdapter(adapter); // this is a custom adapter for the grid which you wont need
}

alertDialogBuilder.setTitle(R.string.flavor_ar); //title of the popup window
alertDialogBuilder.setView(promptView); //set view of the popup to your custom view
alertDialogBuilder.setCancelable(true); //this makes the popup cancelable on back button pressed
alertDialogBuilder.setPositiveButton(R.string.delete_flavor_ar, new DialogInterface.OnClickListener() { //this is a button for the popup if you want one
public void onClick(DialogInterface dialog, int id) {
deleteFlavor = true;
new httpSetFlavor().execute();
}
});
alertDialogBuilder.setNegativeButton(R.string.back_ar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialogFL = alertDialogBuilder.create(); //here you create the dialog using its builder
alertDialogFL.show(); // show it on screen

}

这是我的自定义布局 xml 文件,因此您可以将任何您想要的布局放入其中并稍后使用:)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<GridView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:id="@+id/gridView_flavors"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:numColumns="3"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp" />



<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/gridView_flavors"
android:layout_marginTop="5dp">

<Button
style="?android:attr/buttonStyleSmall"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:singleLine="true"
android:layout_height="wrap_content"
android:text="@string/add_note_ar"
android:id="@+id/button_note"
android:layout_below="@+id/gridView_flavors"
android:textSize="20dp"
android:background="@drawable/button_plain_green"
android:textColor="#000"
android:layout_weight="0" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_note"
android:layout_below="@+id/gridView_flavors"
android:layout_toRightOf="@+id/other_text"
android:layout_marginTop="2dp"
android:layout_toLeftOf="@+id/button_note"
android:layout_toStartOf="@+id/button_note"
android:layout_weight="1"
android:gravity="right" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/other_note_ar"
android:id="@+id/other_text"
android:layout_below="@+id/gridView_flavors"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/editText_note"
android:gravity="center"
android:textSize="20dp"
android:layout_weight="0"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />

<com.andexert.library.RippleView
android:layout_width="wrap_content"
android:id="@+id/ripple_button"
android:layout_weight="0"
android:layout_height="wrap_content"></com.andexert.library.RippleView>
</TableRow>
</RelativeLayout>

这是布局的外观:

enter image description here

在您的情况下,您也可以在自定义布局中使用 ListView :),但使用自定义列表项,例如单选按钮。

至于显示xml中保存的其他语言好吧,你应该将它们保存在 res/strings.xml 中您可以像这样使用字符串数组中的字符串:

String strArray[]={
getResources().getString(R.string.string1),
getResources().getString(R.string.string2),
getResources().getString(R.string.string3),
getResources().getString(R.string.string4)

};

关于java - 如何创建当我单击 ListView (android studio)中的项目时显示的弹出菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com