我是Android新手,想知道如何在LongClick之后在以下某个按钮中向用户显示简单消息。这个想法是在LongClick之后向用户显示他在每个站点上使用的密码。
有什么想法吗?
public class MainMenu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
setTitle("Main Menu");
}
@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, 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);
}
public void openWeb1(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ljmu.ac.uk/")));
}
public void openWeb2(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.bbc.co.uk")));
}
public void openWeb3(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")));
}
public void openWeb4(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.adidas.com")));
}
public void openWeb5(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
}
public void addNew(View v){
}
}
The XML code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/GridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="net.androidbootcamp.myunimanager.MainMenu" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android1:id="@+id/btnLJMU"
android1:layout_width="290dp"
android1:layout_height="76dp"
android1:drawableRight="@drawable/profile"
android1:onClick="openWeb1"
android1:text="Liverpool JMU" />
<Button
android1:id="@+id/btnBBC"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="@drawable/contacts"
android1:onClick="openWeb2"
android1:text="BBC News" />
<Button
android1:id="@+id/btnFACEBOOK"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="@drawable/courses"
android1:onClick="openWeb3"
android1:text="facebook" />
<Button
android:id="@+id/btnADIDAS"
android:layout_width="112dp"
android:layout_height="0dp"
android:drawableRight="@drawable/calendar"
android1:onClick="openWeb4"
android1:text="adidas" />
<Button
android1:id="@+id/btnSTACKOVERFLOW"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="@drawable/notes"
android1:onClick="openWeb5"
android1:text="StackOverFlow" />
<Button
android1:id="@+id/btnADDNEW"
android1:layout_width="match_parent"
android1:layout_height="78dp"
android1:onClick="addNew"
android1:drawableRight="@drawable/myuni"
android1:text="Add New" />
</LinearLayout>
</ScrollView>*
答案 0 :(得分:0)
为按钮设置LongClickListener。在其中,您可以通过弹出来执行相应的操作。
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
检查以下内容以了解如何创建弹出消息。
希望这有帮助!