安卓点击列表项弹出toast提示显示该列表项的文字内容完整代码
时间: 2024-03-11 07:48:58 浏览: 22
好的,以下是一个示例代码:
```java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = ((TextView) view.findViewById(R.id.text_view)).getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
```
其中,`listView` 是你的列表视图,`text_view` 是列表项中显示文本的 TextView 的 id。当用户点击某个列表项时,会触发 `onItemClick` 回调函数,获取该列表项中的文本内容并弹出 Toast 提示。
相关问题
Android popowindow弹出列表 代码
好的,下面是一个简单的例子,演示如何在 Android 中使用 PopupWindow 弹出列表:
1. 首先,在你的布局文件中添加一个 Button 控件,用于触发弹出 PopupWindow。
```
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup" />
```
2. 接下来,在 Java 代码中,获取到 Button 控件,并设置其点击事件。
```
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow(v);
}
});
```
3. 在点击事件的回调中,创建一个 PopupWindow 对象,并设置其内容视图和大小。
```
private void showPopupWindow(View view) {
// 创建一个 PopupWindow 对象
PopupWindow popupWindow = new PopupWindow(this);
// 设置 PopupWindow 的内容视图
View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
popupWindow.setContentView(contentView);
// 设置 PopupWindow 的大小
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 显示 PopupWindow
popupWindow.showAsDropDown(view);
}
```
4. 在弹出的 PopupWindow 中添加一个 ListView 控件,并设置其适配器和数据。
```
private void showPopupWindow(View view) {
// 创建一个 PopupWindow 对象
PopupWindow popupWindow = new PopupWindow(this);
// 设置 PopupWindow 的内容视图
View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
ListView listView = contentView.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"Item 1", "Item 2", "Item 3"}));
popupWindow.setContentView(contentView);
// 设置 PopupWindow 的大小
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 显示 PopupWindow
popupWindow.showAsDropDown(view);
}
```
5. 最后,在列表项的点击事件中,执行相应的操作。
```
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 在这里执行相应的操作
Toast.makeText(MainActivity.this, "Clicked item " + (position + 1), Toast.LENGTH_SHORT).show();
popupWindow.dismiss(); // 关闭 PopupWindow
}
});
```
以上就是一个简单的例子,演示了如何在 Android 中使用 PopupWindow 弹出列表。你可以根据自己的需求进行修改和扩展。
写出Mainactivity文件和布局文件实现:a)文件写入,在输入框中输入内容,在文件名输入框中输入文件名,点击SAVE按钮,弹出文件保存成功信息提示。 b)文件读出,点击READ按钮,弹出app私有目录下的文件列表,选择列表项,读取文件内容和文件名在内容显示框文件名显示框中显示。a)在上述的布局下,增加PICTURE按钮和图片显示控件,点击按钮,显示照片选择页面,选择照片后,将照片现在图片显示控件上。
MainActivity.java:
```
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText editTextContent, editTextFileName;
Button buttonSave, buttonRead, buttonPicture;
TextView textViewContent, textViewFileName;
private static final int REQUEST_CODE_SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextContent = findViewById(R.id.editTextContent);
editTextFileName = findViewById(R.id.editTextFileName);
buttonSave = findViewById(R.id.buttonSave);
buttonRead = findViewById(R.id.buttonRead);
buttonPicture = findViewById(R.id.buttonPicture);
textViewContent = findViewById(R.id.textViewContent);
textViewFileName = findViewById(R.id.textViewFileName);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = editTextContent.getText().toString();
String fileName = editTextFileName.getText().toString();
if (content.isEmpty() || fileName.isEmpty()) {
Toast.makeText(getApplicationContext(), "请输入内容和文件名", Toast.LENGTH_SHORT).show();
} else {
saveFile(fileName, content);
}
}
});
buttonRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileList();
}
});
buttonPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectPicture();
}
});
}
private void saveFile(String fileName, String content) {
File file = new File(getExternalFilesDir(null), fileName);
try {
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
Toast.makeText(getApplicationContext(), "文件保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showFileList() {
File dir = getExternalFilesDir(null);
File[] files = dir.listFiles();
List<String> fileNames = new ArrayList<>();
for (File file : files) {
if (file.isFile()) {
fileNames.add(file.getName());
}
}
String[] fileNameArray = new String[fileNames.size()];
fileNameArray = fileNames.toArray(fileNameArray);
FileListAdapter fileListAdapter = new FileListAdapter(this, fileNameArray);
fileListAdapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
@Override
public void onItemClick(String fileName) {
showFileContent(fileName);
}
});
FileListDialogFragment fileListDialogFragment = new FileListDialogFragment(fileListAdapter);
fileListDialogFragment.show(getSupportFragmentManager(), "fileListDialog");
}
private void showFileContent(String fileName) {
File file = new File(getExternalFilesDir(null), fileName);
try {
InputStream inputStream = getResources().getAssets().open(file.getName());
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String content = new String(buffer, "UTF-8");
textViewFileName.setText(fileName);
textViewContent.setText(content);
} catch (IOException e) {
e.printStackTrace();
}
}
private void selectPicture() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_SELECT_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_PICTURE && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(uri);
}
}
}
```
activity_main.xml:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editTextContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文件内容" />
<EditText
android:id="@+id/editTextFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文件名" />
<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE" />
<Button
android:id="@+id/buttonRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="READ" />
<TextView
android:id="@+id/textViewFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PICTURE" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:visibility="gone" />
</LinearLayout>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)